所以我正在尝试编写一个跟踪余额的基本程序,你可以提款、存款和完全退出程序。这是代码。
#include <iostream>
#include <string>
using namespace std;
double balance = 0, withdraw = 0, deposit = 0;
string choice;
class Bank
{
public:
void withdrawMoney()
{
if(balance - withdraw >= 0)
{
balance = balance - withdraw;
}
else
{
cout << "$5 penalty for attempting to withdraw more than you have.";
balance -= 5;
}
}
public:
void depositMoney()
{
balance = balance + deposit;
}
};
int main()
{
Bank bankObject;
cout << "Welcome to the Bank Program!" << endl;
while(true)
{
cout << "Would you like to make a withdrawal, a deposit, or quit the program: ";
cin >> choice;
if(choice.compare("withdrawal") == 0)
{
cout << "Please enter the amount to withdraw: ";
cin >> withdraw;
bankObject.withdrawMoney();
cout << "New balance is: $" << balance << endl;
}
else if(choice.compare("deposit") == 0)
{
cout << "Please enter the amount to deposit: ";
cin >> deposit;
bankObject.depositMoney();
cout << "New balance is: $" << balance << endl;
}
else if(choice.compare("quit") == 0)
{
break;
}
else
{
cout << "Invalid input." << endl;
}
cout << "Would you like to try again or quit: ";
cin >> choice;
if(choice.compare("quit") == 0)
{
break;
}
}
cout << "Thank you for using the Bank Program." << endl;
return 0;
}
我是 C++ 新手(我从今天开始),但我以前有过 Java 的经验。在第一个 if 语句中,我不断在withdraw 方法中遇到错误,说我对成员函数的使用无效。任何帮助,将不胜感激。另外,不确定是否重要,但我使用的是 IDE Code::Blocks。
编辑:第一个问题已解决,但现在有另一个问题,当我运行代码时,我可以通过一次就好了,但是当我尝试通过再次输入尝试第二次通过时,它会陷入循环并回答第一个问题是“输入错误”。帮助?