When my program runs, the execution window closes immediately. What is wrong? How do I hold it open/pause it?
Nothing is wrong - that is exactly what your program has been designed to do - execute, then exit.
When newcomers ask the question above, they are almost always told to use one of the following commands:
getch(); //or system("pause");
Both of the commands above are platform specific solutions, and do not conform to ANSI standards. They use components that are found on certain architectures, but not others. It is preferable to use a solution that does conform to standards, and uses accepted C++ methods.
One such method that is also often recommended is using:
cin.get();
This solution will indeed pause for user input - as long as no characters are currently in the stream. As most applications will have asked for input several times, it is likely that there will be at least one character in the stream (often a newline) that will be captured by the cin.get() method, and the application will continue on past.
There are many ways to do it and remain standards compliant. I will not reinvent the wheel, but will simply post one example already submitted by a member of this site - Xing (hope you don't mind me posting this).
This simple snippet is as follows:
#include <iostream> #include <limits> int main() { // Rest of the code //Clean the stream and ask for input std::cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' ); std::cin.get(); return 0; }
The original submission can be found here:
http://www.dreaminco.../snippet582.htm
This method is standards compliant, and will hold the command window open without invoking any executables that may or may not be present.
Of course, you can always run the program from the command line.
