我在 C++ 中做了一个基本的“猜我的数字游戏”,并且我添加了一个变量来表示人们猜数字的次数。然而,当游戏结束时,会显示一个随机的猜测值。像 1980046322 之类的东西。考虑到我没有在我的代码中添加任何非同寻常的东西,我无法弄清楚它为什么会这样做。
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
int randomNumber;
int guess;
int guesses;
srand((unsigned)time(0));
randomNumber = (rand()%10)+1;
cout << "I am thinking of a number between 1 to 10. Can you guess it?" << endl;
while(guess != randomNumber){
cout << "That is not correct, try again.";
guesses++;
cin >> guess;
}
if(guess == randomNumber){
cout << "Good Job. Guesses: " << guesses << endl;
guesses++;
}
return 0;
}