I have a problem; I’m a bit new to c++ and am having difficulty with my programs. For example if I was doing some sort of basic code that prints text on the screen like the following:
// program prints hello world
#include %26lt;iostream%26gt;
using namespace std;
int main ()
{
cout %26lt;%26lt; "Hello World!";
return 0;
}
When I run it, it opens for a split second and then vanish again. I have tried this with other, more complex programs, some with options in them but still they open for a split second then vanish. Plz help thanks :P
C++ console programs open and close instantly?
system("pause");
This will make your programs pause, insert it on the line above return 0;
You can actually use system("pause"); at any point in your program to make the program stop and ask the user to press a key to continue.
Reply:Open a DOS prompt and execute the program from there. The "Hello World" is printed on the DOS console screen.
Reply:If you're in Visual Studio, run it with Ctrl+F5 instead of just F5.
The other answers here will work, but this is probably the simplest ;-).
Reply:I think U are using Visual Studio and U are running the application using Run(or start debugging) or F5
What is really happening is, the application control is getting passed back to the operating system after return 0; and the console window is thus getting destroyed.
Including the header file stdio.h and including getchar() before return 0; will make the application to collect character and be alive until keystroke.
Ctrl+F5 will embed additonal code in your binary and make it wait till you enter a key.
Reply:You have to put code into the program to keep it from executing that return 0; until you're ready for it to.
getchar();
or something similar should work, holding the program until you enter something on the keyboard.
Hope that helps.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment