它给了我一个错误,说并不是所有的控制路径都返回值:请帮助。
int using_range()
{
int Num;
try{
cout << "Please enter a integer between 1 and 10: ";
cin >> Num;
if ((Num > 10) || (Num < 0)){
throw 77;
}
else if ((Num <= 10) || (Num >= 0)){
cout << Num << " is in range\n";
return 0;
system("pause");
}
}
catch (int x){
cout << "The number cannot be greater than 10, or less than zero. Error " << x << endl;
system("pause");
return 0;
}
}我不知道该怎么做
发布于 2014-04-12 09:07:45
问题是您有一个后跟else if的if,但是如果这两个检查都失败了,那么默认情况下将不会返回任何内容。您的代码应该防止这种情况发生,但是编译器不够聪明,无法100%确定您永远不会在两个if检查中都失败。
try{
cout << "Please enter a integer between 1 and 10: ";
cin >> Num;
if ((Num > 10) || (Num < 0)){
throw 77;
}
else if ((Num <= 10) || (Num >= 0)){
cout << Num << " is in range\n";
return 0;
system("pause");
}
// <- if you get here, nothing is going to be returned
}另一种方法可能是将else if更改为只使用else
https://stackoverflow.com/questions/23024860
复制相似问题