closing the window

Started by
5 comments, last by Oluseyi 19 years, 6 months ago
#include <iostream> using namespace std; int main() { int pause; cout << "7 + 3 =" << 7 + 3 << endl; cin>>pause; return 0; } whats the simplest statement to make the window close hitting enter key? this one requires clicking the window X to close.
A day without sunshine is like, well...night.
Advertisement
cin.get()

There are other ways, of course, but I've used this one because it is small and cross-platform.
It's platform-specific. There's the non-standard getch function:
#include <conio.h>...  cout << "Press a key to exit." << endl;  getch();...
There's also kbhit:
#include <conio.h>...  cout << "Press a key to exit." << endl;  while(!kbhit()) {}...
And, of course, there's cin.get. Curiously, though, this one (unlike the others) requires the return key. The other two will respond to any key being pressed.
...  cout << "Press [Enter] to exit." << endl;  cin.get();...

Properly, though, you should let the environment handle that. If you're trying to write portable C++ code, then realize that C++ has no concept of "windows" and that it's either your IDE or operating system that is keeping the window open.

In short, ignore it. It's not part of your program.
extremly helpful! thx much!
A day without sunshine is like, well...night.
system("pause"); some may say iostream dosent cover it, it does.
I agree with Oluseyi that this shouldn't really be a part of your program. I only use it when developing console apps in Windows because I'm too lazy to dig through the VS.NET options to see if it will keep the window open for me.
Quote:Original post by dan1088352
system("pause");
There's a problem with the system function, which is that it relies on the user PATH. If someone were to put a binary named pause.exe or pause.com under Windows, or pause under UNIX, anywhere on the path (since pause is a system command handled by the command interpreter), that binary would be executed instead.

Friendly heads up.

This topic is closed to new replies.

Advertisement