Connect with us

Help with C code or windows 7 cmd prompt pop up box

Discussion in 'Microcontrollers, Programming and IoT' started by chopnhack, Sep 4, 2015.

Scroll to continue with content
  1. chopnhack

    chopnhack

    1,573
    354
    Apr 28, 2014
    Hi folks, reading a book on C programming, a modern approach and working through the examples. So far so good, best book on C I have ever read!

    One problem I am having with some of my programs is that they behave differently outside of the IDE. I am using codeblocks, but when I save my program and use it as a stand alone .exe the program doesn't behave the same. What usually happens is the screen under the standalone version will disappear after I hit enter. On some programs, the simpler ones, I was able to circumvent by adding another getchar(); statement right after the first at the end of the program. My guess was the '\n' inside of the printf statement was triggering the getchar to activate. In this next program that is more complex, the same issue was occurring in the windows box vs. the IDE. I even added a series of getchar statements, but it didn't work.

    The program takes an input of a certain dollar amount and breaks it down into the highest denominations of US currency and outputs how many of each bill is required. In the IDE it works flawlessly, producing expected results with a pause at the end to see your work.
    upload_2015-9-3_22-15-13.png
    upload_2015-9-3_22-23-20.png
    As a standalone .exe under windows, after entering the value and hitting enter, the screen disappears. I assume that this is not a programming issue, but an interface issue. Is there a way to pause the screen to view the work? As a standalone program, I would expect the output to stay and wait for a prompt before shutting down!

    Thanks in advance.
     
  2. chopnhack

    chopnhack

    1,573
    354
    Apr 28, 2014
    Quick solution was removing return 0; :oops::rolleyes:

    Isn't the return 0; necessary to inform main that there is no value returned?
     
  3. Harald Kapp

    Harald Kapp Moderator Moderator

    11,702
    2,717
    Nov 17, 2011
    As far as I know this is standard behavior of the IDE: it keeps the console window open to let you inspect input and output of the program even after the program has terminated. Running as a standalone program there is no IDE to keep the console window open and it will close.

    To my knowledge the standard technique is to add the getchar() statement at the end of the program. Even better: add an informative statement (as the IDE does) like "press any key to continue".

    As far as I know return 0 is default. Other return values can be used to signal e.g. an error condition or a result to the calling program (e.g. a shell script).
     
  4. Old Steve

    Old Steve

    734
    169
    Jul 23, 2015
    This is straining my memory a bit, but I'm pretty sure that I used to use getch(), along with the 'Press any key' line as mentioned by Harald.
    I can't for the life of me remember why getch() was a better choice than getchar() though. Maybe it isn't.

    I just had a look for my old collection of console programs to check, but it seems that I've deleted them.

    Edit: Actually, I think the difference might be that getchar() needs an enter press, whereas getch() doesn't. Might be wrong though. My poor old memory......
     
  5. Harald Kapp

    Harald Kapp Moderator Moderator

    11,702
    2,717
    Nov 17, 2011
  6. Old Steve

    Old Steve

    734
    169
    Jul 23, 2015
  7. Old Steve

    Old Steve

    734
    169
    Jul 23, 2015
    I just wrote a quickie to check. getch() does what is wanted. Doesn't appear to need conio.h like I thought, though.
    The other method is:-
    system("pause");

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main(int argc, char *argv[])
    {
    //    system("pause");
        getch();
        return 0;
    }
     
  8. Old Steve

    Old Steve

    734
    169
    Jul 23, 2015
    I just tried the alternative, using getchar(). It does need an 'Enter' press.

    And although it should work, I get an error when I try the system(pause) method.
    "Windows cannot access the specified device......."
    (First my anti-virus claimed it was a Trojan)
     
  9. Supercap2F

    Supercap2F

    550
    150
    Mar 22, 2014
    I'm pretty sure the reason your program is terminating is because you need to flush the input stream before calling getchar. The reason it's not working on this particular program and does on simpler programs, is most likely because you have a scanf before getchar. What is happening is scanf is reading what you typed from the input stream, without clearing it - So when getchar comes along it sees that the input stream is full (from the last thing you entered), it doesn't know any better so it reads it and finds '\n' thus returning and terminating your program. (That's at least how I understand it)

    Try putting a fflush(stdin); right before getchar on line 42 (or if your using a MAC or Linux then make that fpurge(stdin); )
    Dan
     
  10. Old Steve

    Old Steve

    734
    169
    Jul 23, 2015
    getch() should work fine without the need to do an fflush()
    Execution will pause at the getch() until any key is pressed, and then the console window will close immediately.
    conio.h may still be needed, though. In my DevC++ IDE it wasn't, but I think that with others it probably is. I haven't checked, but my stdio.h might be modified.
    I used to use getch() in the Visual C++ IDE too, without problems.

    Edit: or _getch()
     
    Supercap2F likes this.
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-