【发布时间】:2018-07-02 23:09:53
【问题描述】:
我正在尝试使用以下代码使用嵌套三元运算符,但它给出了错误的答案,我不明白错误是什么。
#include <iostream>
using namespace std;
int main()
{
cout<<"Hello World\n";
int age, Result ;
cout<<"Enter age:";
cin>>age;
Result=age<0?-1
:0<=age<=10?0
:11<=age<=18?1
:-2;
cout<<"Result is: "<<Result;
return 0;
}
对于输入年龄 13,其给出的结果为 0,对于输入年龄 20,其给出的结果也为 0。我不明白这是什么错误。你可以帮帮我吗?谢谢你。
【问题讨论】:
-
warning C4804: '<=': unsafe use of type 'bool' in operation
标签: c++ ternary-operator