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


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


source


Place getc () right before returning. The program will only close if you press any key.



+2


source


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


source


you can enter this at the end of main:

system("pause");

      

+1


source


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

+1


source


Simple: write this to stop it

getch();
i.e

main()

{

//////
Your program

/////////

getch();

}

      

+1


source







All Articles