【发布时间】:2015-07-26 16:48:56
【问题描述】:
我的号码生成器有问题。语法明智,一切正常。我主要想用函数来看看它们是否能正常工作。当我运行程序时,会弹出一条消息,说我的变量“guess”没有初始化。任何人都可以提供有关为什么会发生这种情况的见解吗? 另请注意,即使我没有在下面的代码中包含我的库,它们也存在于实际程序本身中。
using namespace std;
int game();
string playAgain();
int main(){
game();
playAgain();
return 0;
}
int game(){
int guess;
int guessesTaken = 0;
int number = rand() % 10 + 1;
int count = 0;
cout << "I am thinking of a number between 1 and 10, can you guess it? " << endl;
while (guessesTaken < count){
cout << "Take a guess: " << endl;
cin >> guess;
if (guess > number)
cout << "Too High. Try again!" << endl;
if (guess < number)
cout << "Too Low! Try again!" << endl;
if (guess == number)
break;
}count++;
if (guess == number)
cout << "Congratulations!" << endl;
return 0;
}
string playAgain(){
string play;
cout << "Want to play again?: " << endl;
if (play == "y" || "Y")
main();
else
cout << "Thanks for playin" << endl;
return 0;
}
【问题讨论】:
-
不管你的问题
}count++;肯定是错的