Exiting program from code?

Started by
16 comments, last by Promit 18 years, 9 months ago
Can you exit your program with code? (Lang C++, Compiler Dev-C++, OS XP)
The best thing to do is just choose whatever you think you'd prefer, and go for it. -Promit
Advertisement
console or windows API?
depends how you want to do it really, if your program has no loop or getch() etc then it will exit itself, but more info is needed really
It has a loop in it, thats why I want to exit the ENTIRE program. Also, it's in console.
The best thing to do is just choose whatever you think you'd prefer, and go for it. -Promit
Well, there is the exit() function, that will force your program to quit, but calling it from deep within the program is dangerous (there are some catches involved, because it doesn't unwind the stack). If at all possible, you should try to exit via returning from main, either by using return codes to flag errors (simple but crude) or by using structured exception handling (more sophisticated, but trickier to design with).

If you want to get out of the middle of a loop, you can simply issue the break; statement, and it will jump out to whatever comes after the loop.
SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.
The standard exit() or abort() function might do what you want.
You can use a bool like this. If you set the bool to false from within the loop, the program will exit.

int main(){  //set up variables etc.  bool b_continue;  while(b_continue)  {    //do stuff  }  //free memory  return 0;}
Quote:Original post by Promit
If you want to get out of the middle of a loop, you can simply issue the break; statement, and it will jump out to whatever comes after the loop.


That's the problem: I have text to print after the loop if all conditions are met, and I don't want to break from the loop and print that text to the console.
The best thing to do is just choose whatever you think you'd prefer, and go for it. -Promit
Then you need to restructure your code. Show us what you have and I'll try to give you some helpful pointers.

But for a quick and dirty way to get what you want, you could break out of the loop using return, which would avoid your loop exit code.
Quote:Original post by Nitage
Then you need to restructure your code. Show us what you have and I'll try to give you some helpful pointers.


Sorry, no can do.
The best thing to do is just choose whatever you think you'd prefer, and go for it. -Promit

This topic is closed to new replies.

Advertisement