tags:

views:

244

answers:

5

I'm learning C++ and I'm using Visual C++ Express and while running this

#include <stdio.h>

int main()
{
    printf("Hello, World!\n");

    return 0;
}

the cmd window closes so fast, I can't see Hello World is there anyway to prevent this?

+2  A: 

Put a getc() right before the return. The program will close only if you press any key.

Havenard
+1  A: 

Yes the common solution is to add a statement that reads input from the keyboard. This call blocks execution till some key is pressed. You can do it with statements like

printf("Hit \"Enter\" to continue\n");  
fflush(stdin); /* Remove anything that is currently in the standard input */  
getchar();     /* Wait for the user to hit the enter key */
olle
+5  A: 

If you press Control + F5, you won't be attached with a debugger - however, it'll stay open with a "Press any key to continue" style message.

Jeff Wilcox
+1 I never thought of it :(
AraK
thanks for the tip
Raptrex
+1  A: 

you can type this at the end of the main:

system("pause");
Kevin
+1  A: 

I just put a breakpoint on the return statement. No code change. :-)

Thanks, i think ill use this
Raptrex