我刚刚从我的项目中创建了一个 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/