Jump to content
  • Advertisement
Sign in to follow this  
Gray00

closing the window

This topic is 5469 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

#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.

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!