C ++ all Multiplication = 0

时间:2014-02-04 01:31:04

标签: c++ math

当我为我的comp var输入任何数字时,它会输出0.我最初尝试使用int并且阅读double看起来就像我想要的那样。我已经尝试将价格变量设为double而不是const double。我必须根据课程订购的数量申请折扣,我只是遗漏了一些任何帮助将不胜感激:

const double price = 99.0; // price per unit
double comp;   // computer variable
double nodiscount = (price * comp);
double discount20 = (price * 0.8) * comp;   // 10-19 20%
double discount30 = (price * 0.7) * comp;   // 20-49 30%
double discount40 = (price * 0.6) * comp;   // 50-99 40%
double discount50 = (price * 0.5) * comp;   // 100 or more 50%

int main()
{
    cout << "How many units sold?" << endl;
    cin >> comp;

    if ( comp >= 0) {
        if ( comp <= 9 ) {
            cout << "Total cost of purchase is " << nodiscount  << endl;
        } else if ( comp >= 10 && comp <= 19) {
            cout << "The total cost with 20% discount is " << discount20 << endl;
        } else if ( comp >= 20 && comp <= 49) {
            cout << "The total cost with 30% discount is " << discount30 << endl;
        } else if ( comp >= 50 && comp <= 99) {
            cout << "The total cost with 40% discount is " << discount40 << endl;
        } else if ( comp >= 100 ) {
            cout << "The total cost with 50% discount is " << discount50 << endl;
        }
    } else {
        cout << "you entered in invalid charechter pleas enter a number.";
    }
}

1 个答案:

答案 0 :(得分:5)

您需要在之后计算comp * whatever ,以确定comp是什么。实际上,您使用默认值comp = 0计算所有折扣。 (请注意,如果comp是未初始化的本地变量,而不是全局变量,则不会默认为0.这将是不确定的,您会得到各种疯狂的废话。)