首页 > 解决方案 > 当我输入等于 totalCost 的双精度时,如果语句不会运行

问题描述

我应该编写一个程序,接收用户想要的每种类型的甜甜圈(常规或花式)的数量,计算价格,然后根据他们支付的金额计算用户应该收到多少回馈。

我已经完成了问题的第一部分,并且可以成功计算出用户想要的甜甜圈的价格。我现在试图告诉用户他们将获得多少零钱,但是当我编写第一个 if 语句以返回如果客户准确支付所欠款项时,它不会将结果打印到屏幕上。在继续下一部分之前,我想弄清楚这部分。

#include <iostream>
using namespace std;

int main() {
    const double TAX = .075;
    const double REGPRICE = 0.75;
    const double REGDOZENPRICE = 7.99;
    const double FANCYPRICE = 0.85;
    const double FANCYDOZENPRICE = 8.49;

    int regular = 0, fancy = 0;
    double price = 0, payment = 0, change = 0;
    double costOfFancy = 0, costOfRegular = 0;

    cout << "Number of regular donuts ordered: " << endl;
    cin >> regular;


    // calculate cost of regular donuts if donut amount is between 0 and 12
     if (regular < 12 && regular > 0) {
        costOfRegular = regular * REGPRICE;
    }
     // calculate cost of regular donuts if donut amount is greater than 12
    else if (regular >= 12) {
        int regDozen = regular / 12;
        int regInd = regular % 12;

        costOfRegular = (regDozen * REGDOZENPRICE) + (regInd * REGPRICE);
    }
     // set regular donut cost to 0 if 0 regular donuts are ordered
    else {
        costOfRegular = 0;
    }


    cout << "Number of fancy donuts ordered: " << endl;
    cin >> fancy;

    // calculate cost of fancy donuts if donut amount is between 0 and 12
    if (fancy < 12 && fancy > 0) {
        costOfFancy = fancy * FANCYPRICE;
    }
    // calculate cost of fancy donuts if donut amount is greater than 12
    else if (fancy >= 12) {
        int fancyDozen = fancy / 12;
        int fancyInd = fancy % 12;

        costOfFancy = (fancyDozen * FANCYDOZENPRICE) + (fancyInd * FANCYPRICE);
    }
    // set fancy donut cost to 0 if 0 fancy donuts are ordered
    else {
        costOfFancy = 0;
    }

    // add cost of fancy donuts and regular donuts together and add tax to calculate final cost
    double totalCost = (costOfFancy + costOfRegular) + ((costOfFancy + costOfRegular) * TAX);
    cout << "Customer owes " << totalCost << endl;

    cout << "Customer pays " << endl;
    cin >> payment;

    if (payment == totalCost) {
        cout << "Exact payment received - no change owed" << endl;
    }





    return 0;
}

例如,如果用户订购了 1 个普通甜甜圈和 1 个精美甜甜圈,则应付金额为 1.72,如果用户输入 1.72 作为付款,则没有任何回报。

标签: c++

解决方案


推荐阅读