下面是一个提示用户输入数字的简单程序。根据输入的值为用户分配不及格、通过、优点或优异等级:
#include <iostream>
using namespace std;
// 0-30 Fail
// 31-40 Pass
// 41-50 Merit
// 51 and over Distinction
int main()
{
int yourScore;
cout << "Please enter your score: " << endl;
cin >> yourScore;
if (yourScore <=30)
{
cout << "Your grade is fail. Better luck next time." << endl;
}
else if (yourScore >30 && <=40)
{
cout << "Your grade is pass. Good." << endl;
}
else if (yourScore >41 && <=50)
{
cout << "Your grade is merit. Well done." << endl;
}
else
{
cout << "Your grade is distinction. Excellent." << endl;
}
return 0;
}
尝试编译以上代码会产生以下错误:
main.cpp|21|error: expected primary-expression before '<=' token
main.cpp|25|error: expected primary-expression before '<=' token
我尝试在 >30 && <=40
周围添加括号和 >41 && <=50
, 这产生了更多的错误。
我哪里错了?
最佳答案
应该是
yourScore>30 && yourScore<=40
假设每个条件都有一个真值或假值。在您的代码中 (<=40) 不是实际的 bool 表达式。你必须要操作数
关于c++ - 错误 : expected primary-expression before '<=' token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9376823/