Jump to content
  • Advertisement
Sign in to follow this  
loachman

Forcing Program Exit

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

Is there a built in function to force program termination within the C++ console. You know how when your program is finished you see "Press any key to continue" Then you exit the console. Is the a built in function to bring up the "Press any key to continue" exit message? Thanks

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by loachman
Is the a built in function to bring up the "Press any key to continue" exit message?


System("Pause"); //no idea what the correct syntax is or if this is even correct =)

If your problem is that the window is closing too fast, the correct solution is to actually run the program from the command console (Start -> run -> cmd). Console apps are meant to be run from the console, when you run them by double-clicking they're supposed to close immediately. Alternatively, if you're running in a debugger, just put a breakpoint at the end of main.

-me

Share this post


Link to post
Share on other sites
Thanks. I just need a way to let the user choose to exit the program like it normally exits. By the "press any key to continue".

I'll keep hunting around

Loachman

Share this post


Link to post
Share on other sites
"normally exiting" means falling through to the bottom of main. Otherwise how can you be sure that any memory you've allocated is cleaned up, etc?

If you're talking about exiting the program entirely, there's no simple way to do that. You should just return from whatever function with a fail message (return false or whatever). Then main knows to exit:


bool doSomething()
{
bool successful = false;

//do that something

return successful;
}

void main()
{
if ( !doSomething )
{
return;
}

doTheRestOfTheProgram();
}





Share this post


Link to post
Share on other sites
Another way would be to have whatever internal function throw an exception (don't remember the syntax for exceptions in C/C++ so you'll have to look it up):


void doSomething()
{
if ( notSucessful )
{
throw exception;
}
}

void main()
{
try
{
doSomething();
}
catch ( exception )
{
//free memory and whatnot then...

return;
}

doTheRestOfTheProgram();
}


Share this post


Link to post
Share on other sites
Loachman... "exit normally" is really the effect you witness (it just disapears when done).
The "IDE" style of exiting is the "press any key to continue".

I highly sugest against the "system ("pause")" way of doing that, because 1) it is overkill slow due to calling the function
pause inside a dos environment. 2) because it isnt portable code.

use something like _getch() or cin.get() as the last line of the program.

Share this post


Link to post
Share on other sites
K thanks for all of your help. I am familiar with _getch(). I think this will pan out well. The functions look like they could be helpful as well.

Loachman

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!