2015-10-14 130 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留下一些樂趣。 :) –