Closes on its own

Started by
6 comments, last by helix 18 years, 10 months ago
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?
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.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Instead of creating a batch file you could also just call pause from inside your program with the system function, like this: system("pause");
could yall exlain how to do both.
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.
Make sure to also include the header file "stdlib" when using the "system" function if you have not already done so.
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..
moe.ron
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.

This topic is closed to new replies.

Advertisement