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;
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:
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;
}
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