折扣代码..我哪里出错?

时间:2017-10-11 15:00:11

标签: c++

此代码旨在根据客户数量提供商品的最终价格。我已经在方程式中添加了但是当它运行时它的折扣和最终价格没有正确评估。如果有人知道我在伪代码中出错了,那将非常有帮助。

using namespace std;
int main() {

    // Variables
    double Retail, quantity, discount1, discount2, discount3, discount4, TotalCost1, TotalCost2, TotalCost3, TotalCost4;
    Retail = 99;
    quantity = 0;
    discount1 = (quantity * Retail) * .20;
    discount2 = (quantity * Retail) * .30;
    discount3 = (quantity * Retail) * .40;
    discount4 = (quantity * Retail) * .50;
    TotalCost1 = quantity - discount1;
    TotalCost2 = quantity - discount2;
    TotalCost3 = quantity - discount3;
    TotalCost4 = quantity - discount4;

    //Equations
    cout << "Please enter the quantity of items you would like to purchase: " << endl;
    cin >> quantity;
    cout << "The number of items being purchased is: " << quantity << endl;

    if (quantity <= 19) {
        cout << "Your final price is: " << discount1 << endl;
    }
    if (quantity >= 20) {
        cout << "Your final price is: " << discount2 << endl;
    }
    if (quantity >= 50) {
        cout << "Your final price is: " << discount3 << endl;
    }
    if (quantity >= 100) {
        cout << "Your final price is: " << discount4 << endl;
    }




        system("pause");
}

1 个答案:

答案 0 :(得分:1)

在计算折扣之前,您的数量是否仅被定义为除0之外的任何数据?我在计算之前移动了您的购买读取并获得了更好的结果(使用在线编译器)。

double Retail, quantity, discount1, discount2, discount3, discount4, TotalCost1, TotalCost2, TotalCost3, TotalCost4;
Retail = 99;
quantity = 0;

//Equations
cout << "Please enter the quantity of items you would like to purchase: " << endl;
cin >> quantity;
cout << "The number of items being purchased is: " << quantity << endl;

discount1 = (quantity * Retail) * .20;
discount2 = (quantity * Retail) * .30;
discount3 = (quantity * Retail) * .40;
discount4 = (quantity * Retail) * .50;
TotalCost1 = quantity - discount1;
TotalCost2 = quantity - discount2;
TotalCost3 = quantity - discount3;
TotalCost4 = quantity - discount4;


if (quantity <= 19) {
    cout << "Your final price is: " << discount1 << endl;
}
if (quantity >= 20) {
    cout << "Your final price is: " << discount2 << endl;
}
if (quantity >= 50) {
    cout << "Your final price is: " << discount3 << endl;
}
if (quantity >= 100) {
    cout << "Your final price is: " << discount4 << endl;
}


Please enter the quantity of items you would like to purchase: 
The number of items being purchased is: 19
Your final price is: 376.2