c++ - 比较 C++ 时常量超出范围

标签 c++ if-statement

我刚刚从我的项目中创建了一个 C++ 问题,我编译了程序并没有给我错误,但给了我一个警告,我无法运行或测试该程序。

#include <iostreamn.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
    int grossincome,totaldeduction,tax,output;

    cout<<"Input gross income: "<<endl;
    cin>>grossincome;
    cout<<"Input total deductions: "<<endl;
    cin>>totaldeduction;

    if (grossincome > 100000)
    {
       tax = (grossincome * .20);
       output = (grossincome - totaldeduction - tax);
    }
    else if((grossincome <= 100000) && (grossincome >= 50000))
    {
       tax = (grossincome * .15);
       output = (grossincome - totaldeduction - tax);
    }
    else if(grossincome < 50000)
    {
       tax = (grossincome * .10);
       output = (grossincome - totaldeduction - tax);
    }
    cout<<"Output: "<<output;
    system("pause");
    return 0;
}

我在所有 If 语句 中都收到警告,并在下面说出消息。

  WARNING message: "Warning Q4.CPP 16: Constant out of range in comparison"
                   "Warning Q4.CPP 21: Constant out of range in comparison"
                   "Warning Q4.CPP 21: Constant out of range in comparison"
                   "Warning Q4.CPP 26: Constant out of range in comparison"

最佳答案

Turbo C/C++ 是一个旧的编译器。 int 的范围是从 -32768 到 +32767。
要么你可以切换到更好的标准编译器(如 gcc),要么使用 long 修饰符。

关于c++ - 比较 C++ 时常量超出范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31244184/

相关文章:

c++ - 在 VS Code 中编译 C/C++

没有舍入的 C++ long double 格式

c++ - 无法使用 Live555 服务器进行流式传输 - 示例不起作用

python - 检查哪个条目被聚焦(Python)

if 条件到三元运算符的转换

c++ - 无法为 Sublime Text 设置 EasyClangComplete

c++ - 如何一次将一个字符串插入堆栈中的一个元素?

JavaScript - 循环内的 If 语句出现问题

javascript - 映射有条件改变的嵌套字段

MySQL 存储函数给出错误(if - else)