我想基于一个可迭代的条件声明,但是我知道条件表达式必须是一个常量。有什么解决方法?
我尝试了以下代码,但仍然无法正常工作。
#include <iostream>
using std::cout;
using namespace std;
int main()
{
int i = 0;
while( i >= 0)
{
const int z = i;
cout << "Enter a number other than " << z << "!\n";
int choice;
cin >> choice;
switch(choice){
case z: cout << "Hey! you weren't supposed to enter "<< z <<"!"; return 0; break;
default: if(i==10)
{
cout << "Wow, you're more patient then I am, you win."; return 0;
}
break;
}
i++;
}
}
case
需要在编译时已知的恒定整数值。
因此,您必须使用if
-s:
if (choice == z) {
cout << "Hey! you weren't supposed to enter "<< z <<"!";
return 0;
} else if (i == 10) {
cout << "Wow, you're more patient then I am, you win.";
return 0;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句