如果/ else总是转到else语句

gin*_*kid 16 c++ if-statement

我正在尝试根据用户输入的金额制作一个确定佣金的功能.它需要用户输入double并使用它来确定它所使用的方程式.但是我写的代码总是转到else语句,我不确定我的条件有什么问题.

double calculate(double s)
{
    double c;
    if (s > 300,000)
    {
        c = 25,000 + (0.15 * (s-300,000));
        cout << "went to if" << endl;
        return c;

    }

    else if (300,000 > s && s > 100,000)
    {
        c = 5,000 + (0.10 * (s-100,000));
        cout << "went to else if" << endl;
        return c;

    }

    else
    {
        c = 0.05 * s;
        cout << "went to else" << endl;
        return c;

    }
} 
Run Code Online (Sandbox Code Playgroud)

Che*_*Alf 25

s > 300,000是一个逗号表达式,相当于(s > 300),000.逗号表达式的值是此处列表中最后一个的值000.false在转换为时进行评估bool.

你可以把它写成

if( s > 300'000 )
Run Code Online (Sandbox Code Playgroud)

或者,如果编译器不支持那种新奇的符号,就像

if( s > 300000 )
Run Code Online (Sandbox Code Playgroud)

或者你可以定义

double const k = 1000;
Run Code Online (Sandbox Code Playgroud)

和写

if( s > 300*k )
Run Code Online (Sandbox Code Playgroud)

同样适用于25 000,10万和5000文字.


Ale*_*exD 18

什么打算作为一个数300,000

if (s > 300,000)
Run Code Online (Sandbox Code Playgroud)

事实上,它是一个奇怪的使用,-operator,它被解析为

if ((s > 300),(000))
Run Code Online (Sandbox Code Playgroud)

并且false一直在结果.相反,试试吧

if (s > 300000)
Run Code Online (Sandbox Code Playgroud)

(else if (300,000 > s && s > 100,000)和其他几个地方一样.)