Hello. I wrote a program which works fine, all i need is to add one more thing. When I ask the user to input an answer (withdraw, balance...) I don't know how to prompt them if they enter something else, such as "golfball" and loop them back to entering the correct one.
Here is what I have:
#include <iostream>
#include <string>
using namespace std;
int main()
{
double withdraw, deposit, balance, newBalance, amount;
char ans;
string choice;
balance = 1000;
cout << "Welcome to Whatever ATM!" << endl;
do
{
cout << "\nWhat would you like to do from the following?\n"
<< "\nWithdraw from account\n"
<< "Deposit money into account\n"
<< "Balance inquiry\n"
<< "Quit\n"
<< "\nPlease select now" << endl;
cin >> choice;
ans = choice[0];
newBalance = balance;
do
{
if ((ans == 'w') || (ans == 'W'))
{
cout << "\nWhat amount would you like to withdraw?" << endl;
cin >> amount;
while (amount > balance)
{
cout << "Sorry, you have insufficient funds.\n"
<< "Please try again: ";
cin >> amount;
}
if (amount <= balance)
{
balance = balance - amount;
newBalance = balance;
cout << "Thank you, your new balance is: " << newBalance << endl;
}
}
if ((ans == 'd') || (ans == 'D'))
{
cout << "\nWhat amount would you like to deposit?" << endl;
cin >> amount;
balance = balance + amount;
newBalance = balance;
cout << "Thank you, your new balance is: " << newBalance <<endl;
}
if ((ans == 'b') || (ans == 'B'))
cout << "Your balance is: " << balance <<endl;
if ((ans == 'q') || (ans == 'Q'))
{
cout << "\nThank you for banking with Whatever ATM!" << endl;
return 0;
}
}
while ((ans == 'w') && (ans == 'W') && (ans == 'd') && (ans == 'D')
&& (ans == 'b') && (ans == 'B') && (ans == 'q') && (ans == 'Q'));
cout << "\nWould you like to make another transaction? (Y/N) ";
cin >> choice;
ans = choice[0];
}
while ((ans == 'y') || (ans =='Y'));
cout << "\nThank you for banking with Whatever ATM!" << endl;
}