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 
}

while(i < MAX_GUESSES, PlayAgain)

If you want to check on two or more conditions in c++ you use && or ||

while( (i < MAX_GUESSES) && PlayAgain)

Edit: I am sorry but ur code is right, it was my first time to know that using of comma is allowed in C++ (my mistake)

while(i < MAX_GUESSES, PlayAgain)

let me have another chance :) , in the above code PlayAgain is never changed, what breaks the loop is 'i' reaching for 'MAX_GUESSES', later you check on 'PlayAgain' but it is already out of the scope of this loop. You understand what I mean?

bool PlayAgain = true;
while(PlayAgain){
// all your code goes here



// last step before the end u check on the user's input whether he wants to play agian or not

//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;

using

while(i < MAX_GUESSES && PlayAgain)

the code runs good, it even now counts up to 10 tries then displays the message.
then it displays the next message "would.... to display your geussed number?" entering y breaks the program. not sure why, I have tried may way.

using your last suggestion the program does not stop after 10 tries.

have u removed the "return 0;" from the if condition?

The code you claim is the problem certainly has a major problem:

if ((i = 10) && (userGuess != RANDOM_NUMBER)){

This says:

set i to 10
is i true?  Yes, it's 10. It's ALWAYS true...

Did you want, perchance, to compare i with 10???? ;-)
Also, do you really want &&?
Finally, all you have is an IF that may output a message. This actually accomplishes nothing but output a message. Don't you want it to do more than that?

Think it through.

I am getting this message:
Unhandled exception at 0x00412266 in Week3_ assignment 3.exe: 0xC0000005: Access violation reading location 0xcccccccc.
with option to "break" or "continue"

and on the actual code I am getting a yellow arrow on

 guesses[k]->outputGuessString();

Sorry WaltP, I didn't see you message!
Honestly I do not know why loop is now work.

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) {

    if ((i = 10) && (userGuess != RANDOM_NUMBER)){
                cout << "\nYou have reached maximum guess allowed!" <<endl;
                cout << endl;
            }

    char again;
    cout << "\nEnter y to display all guessed numbers:  ";
        cin >> userResponse;
        cout <<"\nYou entered: " << endl;
        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();
        }
    }

    //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;
            }

     system ("pause"); // For user to close the window 
}

It doesn't. You haven't address any of the problems I pointed you to.

As for your Access violation, k is probably an invalid value. Or guessValue is not part of the High or Low class.

Honestly I don't know what else to do with my loop. I tried several way none worked.
As for K, i got it figure it.
Thanks for all your feedbacks.

Did you fix if ((i = 10) && (userGuess != RANDOM_NUMBER)){ ??

That still in my code and loop is working now. So all good. Thank you very much.

Sorry I bothered trying to help.... I'll try to remember not to next time.

Oh no! you didn bother me at all. I appreciate your willingness to help. Sincerely

I meant that next time I won't bother to help. I explained that the line is wrong and you completely ignored me. I won't waste my time on your next problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.