How to immediately stop my program

How to immediately stop when I enter -1

#include <iostream>
using namespace std ;

int main()
{
//declate variable
double accountnumber = 0 ;
double beginingnumber = 0.0 ;
double totalcharges = 0.0 ;
double totalcredits = 0.0 ;
double creditslimit = 0.0 ;
double newbalance = 0.0 ;

//enter details
cout<<"Please Enter Account number(-1 to quit): " ;
cin>>accountnumber ;
cout<<"Please Enter Begining balance: " ;
cin>>beginingnumber ;
cout<<"Please Enter Total Charges: " ;
cin>>totalcharges;
cout<<"Please Enter Total credits: " ;
cin>>totalcredits ;
cout<<"Please Enter Credits Limit: " ;
cin>>creditslimit ;

while(accountnumber != -1 )
{
newbalance = (beginingnumber+totalcharges-totalcredits) ;
if(newbalance < creditslimit)
{
cout<<"New Balance is: " <<newbalance<<endl ;
}
else
{
cout<<"New Balance : " <<newbalance<<endl ;
cout<<"Account : " <<accountnumber<<endl;
cout<<"Credits limit : " <<creditslimit<<endl;
cout<<"Balance : " <<newbalance<<endl ;
cout<<"Credits Limit Exceeded "<<endl ;
}
cout<<"Please Enter Account number(-1 to quit): ";
cin>>accountnumber ;
cout<<"Please Enter Begining balance: " ;
cin>>beginingnumber ;
cout<<"Please Enter Total Charges: " ;
cin>>totalcharges;
cout<<"Please Enter Total credits: " ;
cin>>totalcredits ;
cout<<"Please Enter Credits Limit: " ;
cin>>creditslimit;
}while(accountnumber != -1 ) ;

if (accountnumber == -1)
accountnumber = false;


return 0 ;
}
i believe your looking for the exit command

http://www.cplusplus.com/reference/cstdlib/exit/
Use the return keyword. Since you're in main(), return 0; will suffice.

Re. exit() and related functions:
I'm referring to terminate, abort, quick_exit, exit, unexpected, and probably some other ones I'm missing -- standard functions that don't return.

exit() is the "gentlest" of the bunch. It does the most normal work before your program dies, which makes it the least evil of the bunch if you're going to abuse it.

The problem with exit() is that only objects with automatic storage duration are not destroyed. If you're relying on that destruction (you usually are), calling exit() will leave things undone. Even if you aren't relying on it, it would usually be a serious mistake to rely on that fact it in all but the most controlled cases.

If you need to just transfer control elsewhere (give up) and simply returning is not feasible, then throw and optionally catch an exception; this unusual non-local transfer of control is the reason they exist (think about the name). The stack unwinding that takes place as result of a thrown exception invokes the destructors of automatic objects and eliminates the bugs that arise from leaving various things undone.
Last edited on
Hello Yeoh4311,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.

I think what you are looking for is:
under the section //enter details
1
2
cin >> accountnumber;
if (accountnumber == -1) return 0;  // This will exit the program. 


and in the while loop
1
2
cin >> accountnumber;
if (accountnumber == -1) break;  //  This will exit the while loop. 


The two lines of code following the while loop are not needed as you do nothing with that code before exiting the program.

Hope that helps,

Andy
Topic archived. No new replies allowed.