繁体   English   中英

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

[英]Why does my program not return 0 in an if statement?

当我按n询问我是否愿意接受费用时,我的程序没有返回0

我希望程序在我按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;
}

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

char InsufFees;
cin >> InsufFees;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM