C++ Little Game Project issue

Hello Everyone

I have encountered a small problem with my attempt of creating our little numbers game.
The If statement doesnt work correctly. The counter is not updated if the correct guess is made and the cout that confirms the solution is not printed. The loop is ended though.
Ive tried moving the correct guess to the else statement but the problem still occurs.
What would be the correct if statement with the loop condition !=

Thanks for your Help!

#include <iostream>

using namespace std;


//Rules
//1.) Player 1 selects a random Number
//2.) Player 2 needs to guess the Number
//3.) The game will tell Player 2 whether the guess was too low or too high
//4.) The game will count the number of attempts Player 2 made

int main()
{
    int p1Target;
    int p2Guess;
    cout << "Welcome to the game" << endl;
    cout << "P1, Type a number between 1 - 50!" <<endl;
    cin >> p1Target; //Number to be guessed
    cout << "P2, Guess the number P1 thought of '\n' Tipp: It's between 1 & 50"<<endl;
    cin >>p2Guess;

    int numberOfAttempts = 0;


    while (p2Guess != p1Target ){


        if (p2Guess == p1Target){
            cout<< "Correct. How did you know?" <<endl;
            numberOfAttempts++;
            }

        else if (p2Guess < p1Target){
            cout<< "That is too low"<<endl;5
            cout<< "Try again!"<<endl;
            cin>>p2Guess;
            numberOfAttempts++;
            }
        else {
            cout<<"That is too high" <<endl;
            cout<< "Try again!"<<endl;
            cin>>p2Guess;
            numberOfAttempts++;
            }
        }

    cout<< "Number of Attempts " <<numberOfAttempts <<endl;

    return 0;
}

1 Like

Try this:

#include <iostream>

using namespace std;


//Rules
//1.) Player 1 selects a random Number
//2.) Player 2 needs to guess the Number
//3.) The game will tell Player 2 whether the guess was too low or too high
//4.) The game will count the number of attempts Player 2 made

int main()
{
    int p1Target;
    int p2Guess = 0;
    cout << "Welcome to the game" << endl;
    cout << "P1, Type a number between 1 - 50!" <<endl;
    cin >> p1Target; //Number to be guessed
    cout << "P2, Guess the number P1 thought of '\n' Tipp: It's between 1 & 50"<<endl;
    cin >>p2Guess;
    int numberOfAttempts = 1;

    while (p2Guess != p1Target ){


        if (p2Guess == p1Target){
            cout<< "Correct. How did you know?" <<endl;
            numberOfAttempts++;
            }

        else if (p2Guess < p1Target){
            cout<< "That is too low"<<endl;
            cout<< "Try again!"<<endl;
            cin>>p2Guess;
            numberOfAttempts++;
            }
        else {
            cout<<"That is too high" <<endl;
            cout<< "Try again!"<<endl;
            cin>>p2Guess;
            numberOfAttempts++;
            }
        cout<< "CORRECT! Number of Attempts " <<numberOfAttempts <<endl;
        }



    return 0;
}
1 Like

The issue with your counter was that you set the counter to 0 after the first guess…

cout << "P2, Guess the number P1 thought of '\n' Tipp: It's between 1 & 50"<<endl;
    cin >>p2Guess;

    int numberOfAttempts = 0;  // THIS SHOULD ALREADY BE 1

Hope this helps.

1 Like

Thank you Thomas!
Need to place my if cout differently and I really couldn’t see the wrong logic I had with the counter.

Was a great help! I really appreciate it.

1 Like

I’ve come back to this a couple of days later and saw it with different eyes.

Here is a cleaner and correctly running version.

Adjustments:
-Added a loop to make sure a valid number is entered.
-adjusted the integer variable p2Guess.
-moved cin p2Guess into the loop
-cut out repeating code
-moved the cout for a correct guess out of the while loop into the correct scope
-removed the correct condition out of the while loop since its not needed with the initial loop condition.
-moved the numberOfAttempts counter from conditions to the loop to save code.

int main()
{
    int p1Target = 0;
    int p2Guess = -1;
    cout << "Welcome to the game" << endl;
    cout << "P1, Type a number between 1 - 50!" <<endl;
    cin >> p1Target; //Number to be guessed
   
    while (p1Target < 1 || p1Target > 50){
        cout<<"Hey thats not correct, type a number between 1 & 50!"<<endl;
        cin>>p1Target;

    }

    cout << "P2, Guess the number P1 thought of '\n' Tipp: It's between 1 & 50"<<endl;
    int numberOfAttempts = 0;

    while (p2Guess != p1Target){

        cin >>p2Guess;

        if (p2Guess < p1Target){
            cout<< "That is too low. Try Again!"<<endl;
        }

        else if (p2Guess > p1Target) {
            cout<<"That is too high. Try Again!" <<endl;
        }
        numberOfAttempts++;
    }

    cout<< "CORRECT! Number of Attempts " <<numberOfAttempts <<endl;

    return 0;

}