views:

50

answers:

4

Hi,

How can i make that the console, with the output, will not disapear after the program ends in VS 2010 express C++?

i write in c and not in c++, soo i need a function and include path to library.

Thanks

+2  A: 

You can simply poll for input. This performs a block so that the function only returns when the user gives more input - usually enter. If you're on Windows you can also use system("PAUSE").

DeadMG
You have to poll twice if there is already a user input.
wok
+2  A: 

You have a few options:

  • Run the program from command prompt
  • Add a getchar() before you return from main.
  • Add system("pause") before you return from main
codaddict
+1  A: 
int waitforenter(void) {
    int ch;
    puts("press ENTER (maybe twice)");

    /* get rid of a (possibly) pre existing '\n' */
    do {
        ch = getchar();
    } while ((ch != EOF) && (ch != '\n'));

    /* and again */
    if (ch != EOF) do ch = getchar(); while ((ch != EOF) && (ch != '\n'));
    return ch;
}

And then call waitforenter() right before the end of your main() function.

pmg
+1  A: 

Pressing Ctrl+F5 ("Build -> Start Without Debugging") will run the application and automatically wait for a keypress before closing the console. However, as the name says, you do not have a debugger attached then.

Philipp