I have to create a game in which the user starts out with the number 5, then a number between 0 and 10 is randomly generated and the object is to guess if its higher or lower than 5. If they guess correctly, they can continue playing indefinitely. However if they may only guess incorrectly three times before the game is over. The problem with my current code is that it seems to be ending the game after three guesses whether they were correct or not and I haven't managed to figure out the logical errors therein. Any help is greatly appreciated! Here's the current code:
#include <iostream>
#include <time.h> /* time */
using namespace std;
int main()
{ // Statements explaining rules to user, must be displayed before beginning any operations
cout << "This is a very simple number guessing game. Each time you will be given a number of the range 0-10." << endl;
cout << "The objective of the game is to guess whether the next number is going to be higher or not. As simple as that." << endl;
cout << "You have the ability to make no more than 3 incorrect guesses before you lose, so guess wisely." << endl << endl;
cout << "You are starting with number 5. Is the next number higher(write H) or lower(write L) ?" << endl;
int incorrectGuesses = 0; // Number of incorrect guesses starts at 0 for each new game, increasing with each mistake
int correctGuesses = 0; // Number of correct guesses starts at 0 for each new game, increasing with each correct guess
int prevNum = 5, nextNum; // Starting number is 5, next randomly generated number is not set value
//Declare response to user inputs
string CorrectStatement = "Correct! The new number is "; //Turning text string for correct guesses into variable
string IncorrectStatement = "Wrong, you made an incorrect guess! The new number is "; //Turning text string for incorrect guesses into variable
string unaccepted_input = "I'm sorry, the input value is not acceptable. Please enter an H to guess higher or an L to guess lower. Thank you!";
do{
// Appropriate Loop structure
srand ( time(0) ); //
nextNum = rand()%10+0;
// appropriate variable initialization
char choice;
cin >> choice; // User inputs guess of whether next number will be higher or lower
if (choice == 'H') // If user guesses the next number will be lower than the beginning number
{
if (prevNum < nextNum) // If the beginning number is less than the next number
cout << CorrectStatement << nextNum << ". Guess if the next number will be higher or lower!" << endl; // Display statement informing user they guessed correctly
correctGuesses++; // After each correct guess, the count for successful attempts increases (without bound)
if (prevNum > nextNum) // If the beginning number is greater than the next number
cout << IncorrectStatement << nextNum << ". Guess if the next number will be higher or lower!" << endl; // Display statement informing user they guessed incorrectly
incorrectGuesses++; // After each incorrect guess, the count for failed attempts increases
}
if (choice == 'L') // If user guesses the next number will lower than the beginning number
{
if (prevNum > nextNum) // If the beginning number is greater than the next number
cout << CorrectStatement << nextNum << ". Guess if the next number will be higher or lower!" << endl; // Display statement informing user they guessed correctly
correctGuesses++; // After each correct guess, the count for successful attempts increases (without bound)
if (prevNum < nextNum) // If the beginning number is less than the next number
cout << IncorrectStatement << nextNum << ". Guess if the next number will be higher or lower!" << endl; // Display statement informing user they guessed incorrectly
incorrectGuesses++; // After each incorrect guess, the count for failed attempts increases
}
} while (incorrectGuesses < 3); // Operation is only performed when the number of incorrect guesses is fewer than 3
if (incorrectGuesses == 3) // If the number of incorrect guesses reaches 3, the game is over
{
cout << "You've made 3 incorrect guesses ! Game is now over !" << endl; // Statement displayed when user loses the game
cout << "You had " << correctGuesses << " correct guesses before the game was over. Thank you for playing!" << endl; // Statement informing user of success rate
}
return 0;
}
Aucun commentaire:
Enregistrer un commentaire