为什么我的程序在if语句中不返回0?

时间:2018-10-09 03:43:42

标签: c++

当我问我是否要收取费用时,按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;
}

1 个答案:

答案 0 :(得分:2)

InsufFees变量的类型为double,因此cin在尝试读取字符时失败。您需要将InsufFees的类型更改为char

char InsufFees;
cin >> InsufFees;