如何更正我的收银机程序的输出?

时间:2019-03-26 18:38:48

标签: c++

编写一个C ++程序来计算收银机抽屉中的现金。 您要计算抽屉中每种面额的钞票数。 调用一个函数,通过接受面额和钞票数量并返回金额来确定每种面额的金额。该功能还可以接受每种面额的硬币数量和硬币数量并返回金额。

您的程序应写出每种面额,钞票/硬币的数量, 每个美元的金额以及抽屉中的总金额。

同时写入屏幕和输出数据。格式为:

Denomination    Number  Amount
------------------------------
.
.
.
.
.
.
.
------------------------------
Total           $
int main() {

    setprecision (2);
    string d;
    int t;
    int tpenny;
    int tnickel;
    int tdime;
    int tquarter;
    int tone;
    int tfive;
    int tten;
    int ttwenty;
    int tfifty;
    int thundred;

    double penny;
    double nickel;
    double dime;
    double quarter;
    double one;
    double five;
    double ten;
    double twenty;
    double fifty;
    double hundred;

    cout << "Enter denomination and amount of bills/coins" << endl;
    cout << "(i.e. 'penny 5' = 0.05c, 'quarter 3' = 0.75c, 'twenty 2' = $40.00)" << endl;
    cout << "When all amounts have been entered, enter 'done'" << endl;

    while (d != "done") {

        double total = penny + nickel + dime + quarter + one + five + ten + twenty + hundred;

        cin >> d;

        if (d == "done") {

            cout << "Denomination     " << "Number     " << "Amount" << endl;
            cout << "-----------------------------------" << endl;
            cout << "Penny:             " << tpenny << "         " << "$" << penny << endl;
            cout << "Nickel:            " << tnickel << "         " << "$" << nickel << endl;
            cout << "Dime:              " << tdime << "         " << "$" << dime << endl;
            cout << "Quarter:           " << tquarter << "         " << "$" << quarter << endl;
            cout << "One:               " << tone << "         " << "$" << one << endl;
            cout << "Five:              " << tfive << "         " << "$" << five << endl;
            cout << "Ten:               " << tten << "         " << "$" << ten << endl;
            cout << "Twenty:            " << ttwenty << "         " << "$" << twenty << endl;
            cout << "Fifty:             " << tfifty << "         " << "$" << fifty << endl;
            cout << "Hundred:           " << thundred << "         " << "$" << hundred << endl;
            cout << "-----------------------------------" << endl;
            cout << "Total:                       " << "$" << total << endl;
        }
        else {

            cout << "Enter additional currency. (if done, type 'done')" << endl;

            cin >> t;

            if (d == "penny") {
                tpenny = t;
                penny = value (d, t);
            }
            if (d == "nickel") {
                tnickel = t;
                nickel = value (d, t);
            }
            if (d == "dime") {
                tdime = t;
                dime = value (d, t);
            }
            if (d == "quarter") {
                tquarter = t;
                quarter = value (d, t);
            }
            if (d == "one") {
                tone = t;
                one = value (d, t);
            }
            if (d == "five") {
                tfive = t;
                five = value (d, t);
            }
            if (d == "ten") {
                tten = t;
                ten = value (d, t);
            }
            if (d == "twenty") {
                ttwenty = t;
                twenty = value (d, t);
            }
            if (d == "fifty") {
                tfifty = t;
                fifty = value (d, t);
            }
            if (d == "hundred") {
                thundred = t;
                hundred = value (d, t);
            }
        }
    }
    return 0;
}

目前我编写代码的方式有效,但前提是必须为每种货币输入一个值。例如,如果我没有输入5的值,那么我的金额就是一个疯狂的数字,数字的默认值不是0。

您对如何解决此问题有任何想法吗? value也是一个读取字符串和整数(即便士5)并返回货币值的函数。

1 个答案:

答案 0 :(得分:0)

您是否尝试过将所有声明的变量初始化为0?某些语言/编译器集不会试图认为您希望新变量的值为0,并且您将从以前获取该内存位置中的任何值。