Sign in to follow this  
jason1

Closes on its own

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this