I am trying to make a Bank Manager Program. For some reason, it turns into an infinite loop when receiving the wrong input. Whenever I try to have an input of something like "Pizza", it constantly repeats the do-while loop that keeps on printing the same thing. I don't know what else I am able to try to fix my problem. Any help would be appreciated.
#include <iostream>
#include <iomanip>
void ShowBalance(double balance);
double Deposit();
double Withdraw(double balance);
int main() {
double balance = 0;
int choice = 0;
do {
std::cout << "******************\n";
std::cout << "Enter Your Choice:\n";
std::cout << "******************\n";
std::cout << "(1).Show Balance\n";
std::cout << "(2).Deposit Money\n";
std::cout << "(3).Withdraw Money\n";
std::cout << "(4).Exit\n";
std::cin >> choice;
switch (choice) {
case 1:
ShowBalance(balance);
break;
case 2:
balance += Deposit();
ShowBalance(balance);
break;
case 3:
balance -= Withdraw(balance);
ShowBalance(balance);
break;
case 4:
std::cout << "Thanks for visiting! ";
break;
default:
std::cout << "INVALID INPUT\n";
}
} while (choice != 4);
}
void ShowBalance(double balance) {
std::cout << "Balance:$" << std::setprecision(2) << std::fixed << balance
<< std::endl;
}
double Deposit() {
double amount = 0;
std::cout << "Deposit:$";
std::cin >> amount;
if (amount > 0) {
return amount;
} else {
std::cout << "Invalid Deposit Amount\n";
return 0;
}
}
double Withdraw(double balance) {
double amount = 0;
std::cout << "Withdraw:$";
std::cin >> amount;
if (amount > balance) {
std::cout << "Insufficent Funds\n";
return 0;
} else if (amount < 0) {
std::cout << "Invalid Withdraw Amount\n";
return 0;
} else {
return amount;
}
}