【问题标题】:Writing a deposit method in C++, Having trouble [closed]用 C++ 编写存款方法,遇到麻烦 [关闭]
【发布时间】:2015-03-04 05:29:34
【问题描述】:

我正在编写一个方法,它接受要存入的金额值作为参数。如果金额大于 bal(余额),则帐户将更新为新金额。否则它会返回旧的 bal 并退出。这是我的代码:

double withdraw(double amount)
{
    if((bal-amount)<0)
    {
        throw new Exception
        ("There were insufficient funds");
        else
            bal=bal-amount;
        return bal;

    }
}

我在使用 Exception 和 else 语句时遇到错误。

【问题讨论】:

    标签: c++ xcode methods


    【解决方案1】:

    你把{ }放错地方了。

    double withdraw(double amount)
    {
        if((bal-amount)<0)
        {
            throw new Exception("There were insufficient funds");
        }
        else
        {
            bal=bal-amount;
        }
        return bal;
    }
    

    【讨论】:

    • 啊,是的,现在我明白了。哎呀。
    猜你喜欢
    • 1970-01-01
    • 2020-11-02
    • 2019-02-14
    • 1970-01-01
    • 2021-03-24
    • 2020-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多