Hello,
Would you please help me? my code is not executing loop to allow user to play again after user cin>> yes.
also the code suppose to allow user 10 tries to guess the number then stop, I am using this code
if ((i = 10) && (userGuess != RANDOM_NUMBER)){
cout << "\nYou have reached maximum guess allowed!";
}
but it's not working.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <limits>
#include "Assignment3.h"
using namespace std;
GuessingGame::GuessingGame() {
//start random number clock at 0
srand(static_cast<unsigned int>(time(NULL)));
randomNumber = rand() % MAX_NUMBER + 1; // formula for computer to create number
}
void GuessingGame::createNewRandomNumber() {
randomNumber = rand() % MAX_NUMBER + 1; // create new number for user 2nd+ time
}
int GuessingGame::getRandomNumber() const { // computer getting random number
return this->randomNumber;
}
Guess* GuessingGame::doGuess(const int& guess) const {
// cases for to match guess number with random number
if (guess == randomNumber) {
return new Correct(guess);
} else if(guess < randomNumber) {
return new Low(guess);
} else if (guess > randomNumber){
return new High(guess);
} else if (guess > MAX_NUMBER){
cout <<"Guess number must be between 1 and 100!";
}
}
bool Guess::isCorrect() const { return false; };
void Low::outputGuessString() const {
cout << guessValue << " was too low." << endl;
}
void High::outputGuessString() const {
cout << guessValue << " was too high." << endl;
}
void Correct::outputGuessString() const {
cout << guessValue << " was correct." << endl;
}
bool Correct::isCorrect() const { return true; // to allow the program to match numbers
};
int main() {
//hold the constant for the maximum guesses the user can make
const int MAX_GUESSES = 10;
GuessingGame game;
//generate a new random number for the current game
game.createNewRandomNumber();
//initialize constants and variables used for the game
int RANDOM_NUMBER = game.getRandomNumber();
int userGuess;
int i = 0; // guessing game starting point
//create an array of pointers of Guess type
Guess *guesses[MAX_GUESSES] = { NULL };
bool PlayAgain = true; // declaration of play again
while(i < MAX_GUESSES, PlayAgain) {
cout << "Computer generates a random number between 1 and 100. Try to guess it";
cout << "\nPlease enter your guess: ";
cin >> userGuess;
if (!cin) {
cout << "Please guess a number between 0 and " << GuessingGame::max << endl << endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
continue; //jumpt to top of while loop...
}
guesses[i] = game.doGuess(userGuess);
// display the current guess
cout << i + 1 << ": ";
guesses[i]->outputGuessString();
if (guesses[i]->isCorrect()) {
break;
}
//increase the counter i by one for usr to make another guess
i++;
}
int numGuesses = i;
char userResponse = 0;
while (userResponse == 0) {
char again;
cout << "\nEnter 'y' for Yes to display all guessed numbers: ";
cin >> userResponse;
if(!cin || (userResponse != 'y' && userResponse != 'Y') && (userResponse != 'n' && userResponse != 'N')) {
cout << "Please enter Y for yes, N for No" << endl; // message to choose to display guessed numbers
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
continue;
}
//display all the guesses made by the user
for (int k = 0; k <= numGuesses; k++) {
cout << k + 1 << ": ";
guesses[k]->outputGuessString();
}
}
if ((i = 10) && (userGuess != RANDOM_NUMBER)){
cout << "\nYou have reached maximum guess allowed!";
}
//For user to play again
char again;
cout << "Would you like to play again? Enter y or n" << endl;
cin >> again;
if (again != 'y')
{
PlayAgain = false;
return 0;
}
cout << "Thank you for playing"; // final message
system ("pause"); // For user to close the window
}