因此,我正在尝试编写一个能够跟踪余额的基本程序,您可以提取,存款,并完全退出程序。这是代码。
#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经验。我在withdraw方法的第一个if语句中不断收到错误,说我对成员函数的使用无效。任何帮助,将不胜感激。此外,不确定它是否重要,但我使用的是IDE Code :: Blocks。
编辑:第一个问题已修复,但现在还有另一个问题,当我运行代码时,我可以通过它一次就好了,但是当我尝试再次尝试再次尝试时,它会卡在一个循环并将第一个问题回答为“错误输入”。帮助答案 0 :(得分:3)
您使用withdraw
作为全局变量和方法名称。重命名其中一个应修复此特定错误。
编辑:当你输入“再试一次”时,你的程序只读“try”(因为默认是按空格分解输入),在缓冲区中留下“再次”以便从{{1}进行下一次读取}。您可以使用一些调试输出语句来验证它。