I am trying to create a random number guessing game in C++, but it is not working as expected. While the program runs it is bugging out and the input is not working.
int randomnumber();
int main()
{
int tries = 0;
while(true) {
int guess;
cout << "A random number between 1 and 10 has been chosen. Guess the number:" << endl;
cin >> guess;
if(guess>randomnumber()){
cout << "Your guess is too high!" << endl;
tries++;
}
else if(guess==randomnumber()) {
cout << "Correct! The number was " << randomnumber() << endl;
}
else if(guess<randomnumber()) {
cout << "Your guess is too low!" << endl;
tries++;
}
}
return 0;
}
int randomnumber() {
srand(time(NULL));
int number;
number = rand()%10+1;
return number;
}
Here is the output from my program:
A random number between 1 and 10 has been chosen. Guess the number:
4
Your guess is too low!
A random number between 1 and 10 has been chosen. Guess the number:
5
Your guess is too high!
A random number between 1 and 10 has been chosen. Guess the number:
4
Your guess is too low!
A random number between 1 and 10 has been chosen. Guess the number:
5
Correct! The number was 5
I apologize if this question may seem duplicate, but I could not find the answer to my problem on this site and I have this exact same problem in Python as well.
Aucun commentaire:
Enregistrer un commentaire