简体   繁体   English

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

[英]Discount code.. Where did I go wrong?

This code is designed to give the final price of an item based off of the customers quantity.此代码旨在根据客户数量给出商品的最终价格。 I have already added in the equations but when it is ran it the discount and the final price are not evaluating correctly.我已经添加了等式,但是当它运行时,折扣和最终价格没有正确评估。 If someone knows where I went wrong in the pseudocode, it would be very helpful.如果有人知道我在伪代码中哪里出错了,那将非常有帮助。

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");
}

Is it just that you're quantity isn't defined as being anything except 0 before your calculate the discounts?只是在计算折扣之前,您的数量没有被定义为除 0 以外的任何东西吗? I moved the read of your purchase before the calculations and got better results (using online compiler).我在计算之前移动了您购买的阅读并获得了更好的结果(使用在线编译器)。

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

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

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