• Advertisement
Sign in to follow this  

Closes on its own

This topic is 4584 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

Whenever I run a program it immediatly closes down. When it does this I add: std::cin.ignore(std::cin.rdbuf()->in_avail() + 1); and it stays open. But now I've made my first program where the user types stuff in to affect the game and it closes on its own. I type the above in to stop it but it still closes. Is there another way to stop this other than what i do?

Share this post


Link to post
Share on other sites
Advertisement
Manually open a console window and run your program there. That way, the operating system won't close the window when the program terminates.

Alternatively, create a batch file that runs your program and then calls "pause". Execute that batch file instead of calling your program directly.

std::cin.rdbuf()->in_avail() returns the number of characters available in std::cin's internal stream buffer. Reading more characters will force the program to query the device (console) for more character, but may still not block if those characters are available in the device.

Share this post


Link to post
Share on other sites
Instead of creating a batch file you could also just call pause from inside your program with the system function, like this: system("pause");

Share this post


Link to post
Share on other sites
Just add system("pause") where you want your program to pause. system is a function that takes a string and executes it, as if you had typed it in a dos prompt. In this case it calls pause, which pauses until you press a key.

Share this post


Link to post
Share on other sites
Make sure to also include the header file "stdlib" when using the "system" function if you have not already done so.

Share this post


Link to post
Share on other sites
or just do this (since it looks like your using c++ stuff)..it will wait for you to hit a key

while( !std::cin.get() ){}


hth..

Share this post


Link to post
Share on other sites
Quote:
Original post by Fruny
Manually open a console window and run your program there. That way, the operating system won't close the window when the program terminates.


A batch file is probably overkill. I would definately use this suggestion above. Go to your start menu, click on run, and type in cmd (or command if you are on win98 or similar). Now from there, browse to where your exe is and run it from there.

For many of my programs written for college classes, I put in some sort of a loop that would ask if you want to exit the program before just closing down.

Share this post


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

  • Advertisement