Keep cmd open when running file
I am learning C ++ and I am using Visual C ++ Express and at runtime this
#include <stdio.h>
int main()
{
printf("Hello, World!\n");
return 0;
}
the cmd window closes so quickly, I can't see Hello World is there anyway to prevent this?
+2
Raptrex
source
to share
6 answers
If you press Control + F5, you will not be attached to the debugger, but it will remain open with a "Press any key to continue" style message.
+5
Jeff wilcox
source
to share
Place getc () right before returning. The program will only close if you press any key.
+2
Havenard
source
to share
Yes, the general solution is to add an instruction that reads keyboard input. This call blocks execution until any 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 */
+1
olle
source
to share
you can enter this at the end of main:
system("pause");
+1
Kevin Doyon
source
to share
I just put a breakpoint on the return statement. No code change. :-)
+1
Peter cardona
source
to share
Simple: write this to stop it
getch();
i.e
main()
{
//////
Your program
/////////
getch();
}
+1
Sunjava1
source
to share