Your program should not care about the console window. It is safe (both in terms of security and of user interaction) to assume that.
On Windows systems, if you run an exe with the "I'm a console program" flag set, Windows will do one of two things:
(1) if your user started the exe from a console window, it will run in that window and terminate, leaving the window as it was.
(2) if your user started the exe any other way (like clicking on it from an Explorer window or in the Start menu), it will spawn a new console window subprocess and attach it to your program's I/O. When your program terminates, the console will disappear.
Modern Unix WMs will behave very much the same. Some older ones may be too stupid to figure things out either
(1) at the start, requiring your user to start an xterm then run your program
(2) at the end, leaving the xterm running after your program has terminated
There is really nothing you can do in your program to fix either of those (minor, even if annoying) problems.
I don't have access to a Mac, but IIUC, it will spawn a console window and destroy it automatically when your program terminates.
The only real issue is those systems that may close your console window before you want it to. That is where things like this come in:
http://www.cplusplus.com/articles/iw6AC542/
The simplest, most cross-platform way would be to just say
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
int PressEnterToQuit()
{
cout << "Press ENTER to end the program." << flush;
string s;
getline( s );
return 0;
}
int main()
{
cout << "Hey, I'm the main() program!\n";
return PressEnterToQuit();
}
|
Hope this helps.