Sign in to follow this  
orcfan32

Exiting program from code?

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Fine, in that case use a break statement in conjunction with a boolean, like this:


int completedTask = 0;
while (1){
doStuff();
if ((completedTask = completedEverything()) || failedTask()){
break;
}
}
if (competedTask){
printStuff();
}



In this example, doStuff() does something, I don't care what it does, nor should you. As for completedEverything() and failedTask(), they return booleans (or integers in this case), and if completedEverything() returned true, completedTask would have been set to true, but if either function returned true, it will break from the loop.

The result: printStuff() will only be called if completedEverything() returns true, but the loop will continue on indefinately unless either completedEverything() or failedTask() return true.

Share this post


Link to post
Share on other sites
Quote:
Original post by Oberon_Command
Quote:
Original post by orcfan32
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.


Why not?


Because he's writing the next generation 3D FPS-RPG-RTS MMO and his code is top-secret. In order for you to view it, you have to sign an NDA and pay him $500,000 to gain access to the source code.

Orcfan: Nobody is able to help you if you don't show some code. If you want to exit your application half way during runtime, just use flags and what-not.

Toolmaker

Share this post


Link to post
Share on other sites
Quote:
Original post by Promit
...by using structured exception handling...
Did you mean C++ exception handling? SEH is a Windows system functionality and Microsoft extension to C (via the __try, __except and __finally statements), and not applicable to any programmer who hopes to keep his code portable.

I know you know this, but your audience might not.

Share this post


Link to post
Share on other sites
Quote:
Original post by Toolmaker
Because he's writing the next generation 3D FPS-RPG-RTS MMO and his code is top-secret. In order for you to view it, you have to sign an NDA and pay him $500,000 to gain access to the source code.

Orcfan: Nobody is able to help you if you don't show some code. If you want to exit your application half way during runtime, just use flags and what-not.

Toolmaker


LOL... He's right, orcfan. Show us some code, and we'll be able to help you.

Share this post


Link to post
Share on other sites
Quote:
Original post by orcfan32
Can you exit your program with code? (Lang C++, Compiler Dev-C++, OS XP)


Here is my opinion for a console application(this is my speciality):
Take a global variable.
Take a interrupt function to control the application and the variable.

If the value of the variable changes main() will break and return a result.
this is the most simple and efficient exit program with a exit code


I usually use this for my applications and i think is the most fastest




PS sorry for my englesh

Share this post


Link to post
Share on other sites
Quote:
Original post by Toolmaker
Because he's writing the next generation 3D FPS-RPG-RTS MMO and his code is top-secret. In order for you to view it, you have to sign an NDA and pay him $500,000 to gain access to the source code.


ROFL! It was a game I was writing, but I discontinued it. Nevermind. It was Tic-Tac-Toe, and it didn't work right, but I was fixing it little by little. Here's the source code anyways:
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
// Declaration of constants
const char PlayerX = 'X';
const char PlayerO = 'O';

// Declaration of variables
char BoardSpaces[10] = {'0','1','2','3','4','5','6','7','8','9'};

char CurrentPlayer = 'X';

int Moves = '0';
int CMove = '0';
bool Win = false;

// Set up the board:
cout<<"\n\n "<<BoardSpaces[1]<<" | "<<BoardSpaces[2]<<" | "<<BoardSpaces[3]<<"\n";
cout<<" ---+---+---\n";
cout<<" "<<BoardSpaces[4]<<" | "<<BoardSpaces[5]<<" | "<<BoardSpaces[6]<<"\n";
cout<<" ---+---+---\n";
cout<<" "<<BoardSpaces[7]<<" | "<<BoardSpaces[8]<<" | "<<BoardSpaces[9]<<"\n\n";

while ( Win == false )
{
cout<<"Player "<<CurrentPlayer<<"'s turn: ";
cin>>CMove;
if ( CurrentPlayer == 'X' )
{
BoardSpaces[CMove] = CurrentPlayer;
CurrentPlayer = 'O';
cout<<"\n\n";
// Re-draw the board:
cout<<"\n\n "<<BoardSpaces[1]<<" | "<<BoardSpaces[2]<<" | "<<BoardSpaces[3]<<"\n";
cout<<" ---+---+---\n";
cout<<" "<<BoardSpaces[4]<<" | "<<BoardSpaces[5]<<" | "<<BoardSpaces[6]<<"\n";
cout<<" ---+---+---\n";
cout<<" "<<BoardSpaces[7]<<" | "<<BoardSpaces[8]<<" | "<<BoardSpaces[9]<<"\n\n";
Moves = Moves + 1;
if ( Moves <= 8 )
{
if ( BoardSpaces[1] == 'X' && BoardSpaces[2] == 'X' && BoardSpaces[3] == 'X' )
{
Win = true;
}
if ( BoardSpaces[1] == 'X' && BoardSpaces[4] == 'X' && BoardSpaces[7] == 'X' )
{
Win = true;
}
if ( BoardSpaces[3] == 'X' && BoardSpaces[6] == 'X' && BoardSpaces[9] == 'X' )
{
Win = true;
}
if ( BoardSpaces[7] == 'X' && BoardSpaces[8] == 'X' && BoardSpaces[9] == 'X' )
{
Win = true;
}
if ( BoardSpaces[2] == 'X' && BoardSpaces[5] == 'X' && BoardSpaces[8] == 'X' )
{
Win = true;
}
if ( BoardSpaces[4] == 'X' && BoardSpaces[5] == 'X' && BoardSpaces[6] == 'X' )
{
Win = true;
}
if ( BoardSpaces[1] == 'X' && BoardSpaces[5] == 'X' && BoardSpaces[9] == 'X' )
{
Win = true;
}
if ( BoardSpaces[3] == 'X' && BoardSpaces[5] == 'X' && BoardSpaces[7] == 'X' )
{
Win = true;
}
else
{
cout<<"\n\nTie game; niether player won!";
Sleep(2000);
exit(1);
}
if ( CurrentPlayer == 'O' )
{
BoardSpaces[CMove] = CurrentPlayer;
CurrentPlayer = 'X';
cout<<"\n\n";
// Re-draw the board:
cout<<"\n\n "<<BoardSpaces[1]<<" | "<<BoardSpaces[2]<<" | "<<BoardSpaces[3]<<"\n";
cout<<" ---+---+---\n";
cout<<" "<<BoardSpaces[4]<<" | "<<BoardSpaces[5]<<" | "<<BoardSpaces[6]<<"\n";
cout<<" ---+---+---\n";
cout<<" "<<BoardSpaces[7]<<" | "<<BoardSpaces[8]<<" | "<<BoardSpaces[9]<<"\n\n";
Moves = Moves + 1;
if ( Moves <= 8 )
{

if ( BoardSpaces[1] == 'O' && BoardSpaces[2] == 'O' && BoardSpaces[3] == 'O' )
{
Win = true;
}
if ( BoardSpaces[1] == 'O' && BoardSpaces[4] == 'O' && BoardSpaces[7] == 'O' )
{
Win = true;
}
if ( BoardSpaces[3] == 'O' && BoardSpaces[6] == 'O' && BoardSpaces[9] == 'O' )
{
Win = true;
}
if ( BoardSpaces[7] == 'O' && BoardSpaces[8] == 'O' && BoardSpaces[9] == 'O' )
{
Win = true;
}
if ( BoardSpaces[2] == 'O' && BoardSpaces[5] == 'O' && BoardSpaces[8] == 'O' )
{
Win = true;
}
if ( BoardSpaces[4] == 'O' && BoardSpaces[5] == 'O' && BoardSpaces[6] == 'O' )
{
Win = true;
}
if ( BoardSpaces[1] == 'O' && BoardSpaces[5] == 'O' && BoardSpaces[9] == 'O' )
{
Win = true;
}
if ( BoardSpaces[3] == 'O' && BoardSpaces[5] == 'O' && BoardSpaces[7] == 'O' )
{
Win = true;
}
else
{
cout<<"\n\nTie game; niether player won!";
Sleep(2000);
exit(1);
}
}
}
}
cout<<"\n\n"<<CurrentPlayer<<" is the winner!";
cin.get();
}
}
}



Then I realized, I bit off more than I could chew.. :(

Share this post


Link to post
Share on other sites
Quote:
Original post by Oluseyi
Quote:
Original post by Promit
...by using structured exception handling...
Did you mean C++ exception handling? SEH is a Windows system functionality and Microsoft extension to C (via the __try, __except and __finally statements), and not applicable to any programmer who hopes to keep his code portable.

I know you know this, but your audience might not.


Yeah, I did mean C++ exception handling. Misspeak on my part.

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