我正在完成我的学校项目。我写了一切,但似乎有一个错误:
缺少";"在cout之前的第15行之前"你的号码更大 比..."
我无法理解此错误或修复它。请帮忙。整个程序的代码如下:
#include <iostream>
using namespace std;
int main()
{
int intSecretNum = 47;
int intGuess;
cout<< "Guess the secret number (between 0 and 100 inclusively): ";
cin>> intGuess;
while(intSecretNum != 47, intGuess<= 5)
{
if( intGuess < intSecretNum)
cout << "Your Number is smaller than the secret number";
else (intGuess > intSecretNum)
cout << "Your Number is bigger than the secret number";
intGuess++;
if(intGuess>5)
cout << "Sorry, you have used up all your quota (5 times)! The secret number is "<<intSecretNum;
cout << "program terminated." <<endl;
}
cout<< "You have used "<<intGuess <<"to guess te secret number which is " <<intSecretNum<<".";
cout<<"program terminated."<<endl;
return 0;
}
我对这个C ++很陌生,请帮忙
答案 0 :(得分:3)
你的问题在这里
else (intGuess > intSecretNum)
cout << "Your Number is bigger than the secret number";
这假定(intGuess > intSecretNum)
是一个在else
条件下执行的语句,这就是为什么你会收到一个错误警告你缺少分号的原因。
相反,你应该
else if(intGuess > intSecretNum)
cout << "Your Number is bigger than the secret number";
答案 1 :(得分:1)
只需更改此行:
else (intGuess > intSecretNum)
到
else if( intGuess > intSecretNum)
问题在于,您不能仅使用关键字else
来提供特殊条件。 Else用于描述“在任何其他情况下......”。因此,在您的情况下,因为它是一个简单的测试,您可以简单地尝试摆脱其他情况之后的条件并从以下地址开始:
else(intGuess&gt; intSecretNum) 到:
else
为了便于阅读,我还建议您始终用{}
包围您的说明
因此,这将给你:
while(intSecretNum != 47, intGuess<= 5)
{
if( intGuess < intSecretNum){
cout << "Your Number is smaller than the secret number";
}
else {
cout << "Your Number is bigger than the secret number";
}
intGuess++;
if(intGuess>5)
cout << "Sorry, you have used up all your quota (5 times)! The secret number is "<<intSecretNum;
cout << "program terminated." <<endl;
}
注意:在你的while循环中也有一些没有用的东西。但这可能是另一个问题的主题
答案 2 :(得分:0)
你的意思是:
while(intSecretNum != 47, intGuess<= 5)
{
if( intGuess < intSecretNum)
cout << "Your Number is smaller than the secret number";
else if(intGuess > intSecretNum)
cout << "Your Number is bigger than the secret number";
intGuess++;
if(intGuess>5) {
cout << "Sorry, you have used up all your quota (5 times)! The secret number is "<<intSecretNum;
cout << "program terminated." <<endl;
}
}
请注意if
之后的else
。 else
没有像if
这样的条件。另请注意最后if
- 块周围的括号。没有括号,只有一个语句使整个if
- 阻止。如果您需要更多,则必须将该块括在括号中。