我正在写经典的“猜猜我的号码”节目。
我似乎不明白为什么即使在我写了“Success”并进入默认路由之后,while循环也不会停止工作。
int xRan;
void rdm(int to, int from){
srand(time(NULL));
xRan = rand()%to+from;
}
void iGuess(){
string b;
int tries = 1;
cout << "Think of a number between 1 and 100" << endl;
rdm(100, 1);
while(b != "Success" || b != "success"){
cout << "is your number higher or lower than " << xRan << ". (attempt #" << tries << ")" << endl;
cout << "If I guessed your number, please type 'Success' " << endl;
cout << "-->";
cin >> b;
if(b == "Lower" || b == "lower"){
rdm(xRan, 1);
tries++;
}else if(b == "Higher" || b == "higher"){
rdm(100, xRan);
tries++;
}else{
cout << "This is not a valid choice." << endl;
}
}
cout << "I'm so good! I did it in " << tries << "attempts!" << endl;
}
答案 0 :(得分:4)
你永远不会离开你的for循环,因为你正在检查它是否是&#34;成功&#34; 或&#34;成功&#34;。任何一个都可以是真的并进入循环。因此,无论您是否使用大写字母输入,其中一个都是真的。而是写
while(b != "Success" && b != "success")