2015-10-14 103 views
1

正在这里做学校项目并遇到问题 我的程序支持用户猜测一个数字是47,如果他们得到了错误,他们总共5次尝试5次。如果仍然没有得到它,程序将退出。如果他们得到了答案,将会有一条消息说明有多少次尝试才能得到答案。 因此,我必须有一个标识符的尝试次数。但C++说,我没有初始化标识符。请帮助标识未被初始化

#include <iostream> 
using namespace std; 

int main() 
{ 
    int intSecretNum = 47; 
    int intGuess; 
    int GuessNum; 
    cout<< "Guess the secret number (between 0 and 100 inclusively): "; 
    cin>> intGuess; 
while(intSecretNum != 47, GuessNum<= 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"; 
     GuessNum++; 
     if(GuessNum>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 "<<GuessNum <<" to guess te secret number which is " <<intSecretNum<<"."; 
cout<<"program terminated."<<endl; 
return 0; 
} 

请帮= d

+0

编译器是正确的。 'GuessNum'没有初始化。此外,'while(intSecretNum!= 47,GuessNum <= 5)'不正确。使用'&&'代替逗号。 – owacoder

回答

4

你忘了初始化GuessNum

int GuessNum = 0; 
      ^^^ 

此外,在while条件逗号没有做什么,你觉得这样做,你的意思是&&在C++中是“和”。

+1

而'cout <<“程序已终止。” << endl;'不在条件内。 –

+1

@BillLynch给OP留下一些乐趣。 :) –