当我问我是否要收取费用时,按0
时,程序没有返回n
。
我希望在按n
时停止程序,并在按y
时运行25美元的费用
有人可以向我解释我在做什么错吗?
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
using namespace std;
int main()
{
double InitialAmount, WithdrawAmount;
cout << fixed << setprecision(2);
cout << "Deposite initial amount: ";
cin >> InitialAmount;
if (cin.fail())
{
cin >> InitialAmount;
cout << "Invalid initial amount" << endl;
return 0;
}
if (InitialAmount < 0)
{
cout << "Balance under 0, cannot withdraw" << endl;
return 0;
}
cout << "Enter an amount to withdraw: ";
cin >> WithdrawAmount;
if (cin.fail())
{
cin >> WithdrawAmount;
cout << "Invalid withdraw amount" << endl;
return 0;
}
if (WithdrawAmount > 500)
{
cout << "Cannot withdraw this amount" << endl;
return 0;
}
double YesFees, InsufFees;
if (InitialAmount > 1 and WithdrawAmount > InitialAmount)
{
cout << "Insufficient funds for this withdrawal There will be a $25.00 service charge. Would you like to continue? (Y/N): ";
cin >> InsufFees;
if (InsufFees == 'y')
{
YesFees = 25;
}
if (InsufFees == 'n')
{
return 0;
}
}
double fees;
if (WithdrawAmount > 300)
{
fees = WithdrawAmount * .04;
}
cout << left << setw(20) << setfill('.') << "Amount withdrawn";
cout << "$" << setw(10) << setfill (' ') << right << WithdrawAmount << endl;
cout << left << setw(20) << setfill('.') << "Amount of fees";
cout << "$" << setw(10) << setfill (' ') << right << fees + YesFees << endl;
cout << left << setw(20) << setfill('.') << "Balance";
cout << "$" << setw(10) << setfill (' ') << right << InitialAmount - WithdrawAmount - YesFees - fees << endl;
return 0;
}
答案 0 :(得分:2)
InsufFees
变量的类型为double
,因此cin
在尝试读取字符时失败。您需要将InsufFees
的类型更改为char
:
char InsufFees;
cin >> InsufFees;