How can i get this program to continue as many times?

How can i get this program to continue as many times as the user wants.



#include <iostream>


#include <string>

using namespace std;
int main()
{
cout<<"Welcome! You have 5000 dollars in your account. Here are your options: Enter (D) or (d) to make a deposit, (W) or (w) to make a withdrawal, (B) or (b) to view your current balance and (Q) or (q) to quit."<<endl;

string choice;
float deposit;
float withdraw;
float balance = 5000.00;

cout<<"Please enter your choice."<<endl;
cin>>choice;

if (choice == "D" || choice == "d")
{

cout<<"please enter the amount you want to deposit"<<endl;
cin>>deposit;

while (deposit <1)
{
cout<<"please enter proper amount"<<endl;
cin>>deposit;
}
(deposit >1);
{
cout<<"your currnet balance is"<<balance + deposit<<endl;
cin>>balance;
}
}
if (choice == "W" || choice == "w")
{

cout<<"please enter the amount you want to withdraw"<<endl;
cin>>withdraw;

while (withdraw<1)
{
cout<<"please enter proper amount"<<endl;
cin>>withdraw;
}
(withdraw > 1);
{
cout<<"your currnet balance is"<<balance - withdraw<<endl;
cin>>balance;
}
}
if (choice == "B" || choice == "b")
{
cout<<"your current balance is"<<balance<<endl;
cin>>balance;
}

if (choice == "Q" || choice == "q")
{
return 0;
}
}
Something you could do is this:
1
2
3
4
while(true) // loops endless
{
    if( YouWantToExitTheLoop ) break; // the loop is terminated
}

However, being your code simple, you could just add the while(true) part as when return 0; is reached the program is terminated so, in your case:
1
2
3
4
5
6
7
8
int main()
{
   //declarations
   while(true)
   {
         //the remaining code you had in main()
   }
}
Last edited on
To do that, nest everything starting with the first cin>>choice under a while loop (something simple like while (true) ), and have choosing q break the loop.

1
2
3
4
5
6
 cout<<"Please enter your choice: "<<endl;
 while (true) {
    cin >> choice;
      if (choice  == "Q" || choice == "q") 
        break;
   }


On another note, I don't know how you want the banking system to work, but the way it is in the source code, there isn't any depositing or withdrawing going on.
For instance, this:

cout<<"your currnet balance is"<<balance - withdraw<<endl

doesn't subtract the withdrawal from the deposit, it just displays the difference between the two if you did subtract them. To actually subtract from the balance you should either do:

1
2
3
4
5
6
7
        cout<<"your currnet balance is"<<balance - withdraw<<endl;
         balance -= withdraw;
       //or 
       balance -= withdraw;
       cout<<"your currnet balance is"<<balance<<endl;
       //or even this (untested and unrecommended)
       cout<<"your currnet balance is"<<balance -= withdraw, balance<<endl;


You also need to check that the withdrawal value is less the balance.

Here's how I would handle withdrawal:

1
2
3
4
5
6
7
8
if (choice == "W" || choice == "w") {
   do {
     cout<<"Please enter the amount you want to withdraw: "<<endl; 
      cin>>withdraw;
   } while(withdraw < 1 && withdraw <= balance);
   cout<<"Your currnet balance is "<<balance - withdraw<<endl;
   balance -= withdraw;
   }


Hope I hoped a little.

EDIT: Bah, beat to the punch!
Last edited on
You could use a loop for tht purpose. And ensure that it continues for as many times the user wants it to. For that u can use a break statement in an if condition.
Hope it works
Topic archived. No new replies allowed.