Help with My Little Game Code

I am wondering why I am not able to print out on the client within my if statement can anyone help me?

int main()
{
    int startNum = 0;
    int guessNum = 0;
    int numTries = 0;
    cout << "Welcome to the guessing game! Player 1 enter your number in from 1 - 100" << endl;
    cin >> startNum;
    cout << "Player  2 guess the number Player 1 picked from 1 - 100" << endl;
    cin >> guessNum;
    while (guessNum != startNum){
        if (guessNum > startNum){
            cout >> "Number too high! Try again!" >> endl;
            cin << guessNum;
            ++numTries;
        }
        if else (guessNum < startNum){
            cout >> "Number too low! Try again!" >> endl;
            cin << guessNum;
            ++numTries
        }
        if else (guessNum == startNum) {
            cout >> "That is correct!" >> endl;
            ++numTries;
            return numTries;
        }
        else{
            cout >> "Error: Input does not follow rules!" >> endl;
        }
    }
    cout >> "Number of attempts to find the answer: " >> numTries >> endl;
    return 0;
}

#include

int main()
{
int startNum = 0;
int guessNum = 0;
int numTries = 0;
std::cout << “Welcome to the guessing game! Player 1 enter your number in from 1 - 100” << std::endl;
std::cin >> startNum;
std::cout << “Player 2 guess the number Player 1 picked from 1 - 100” << std::endl;
std::cin >> guessNum;
while (guessNum != startNum){
if (guessNum > startNum){
std::cout << “Number too high! Try again!” << std::endl;
std::cin >> guessNum;
++numTries;
}
else if (guessNum < startNum){
std::cout << “Number too low! Try again!” << std::endl;
std::cin >> guessNum;
++numTries;
}
else if (guessNum == startNum) {
std::cout << “That is correct!” << std::endl;
++numTries;
return numTries;
}
else{
std::cout << “Error: Input does not follow rules!” << std::endl;
}
}
std::cout << "Number of attempts to find the answer: " << numTries << std::endl;
return 0;
}

This works, after you fix all the syntax errors that come from posting the code as text instead of using a codeblock :sweat_smile:

1 Like

Hi @DJaySplash - 3 issues with the code

#1 you have the << operator round the wrong way in your cout in the if statements

#2 you missed a ; on one of the ++numTries

#3 should be else if, not if else

All the best - Mark (ex-C++ trainer!!)

@Alko89 - thank you - I will use code block - may I practice here…

#include <iostream>


int main()
{
    int startNum = 0;
    int guessNum = 0;
    int numTries = 0;
    std::cout << "Welcome to the guessing game! Player 1 enter your number in from 1 - 100" << std::endl;
    std::cin >> startNum;
    std::cout << "Player  2 guess the number Player 1 picked from 1 - 100" << std::endl;
    std::cin >> guessNum;
    while (guessNum != startNum){
        if (guessNum > startNum){
            std::cout << "Number too high! Try again!" << std::endl;
            std::cin >> guessNum;
            ++numTries;
        }
        else if (guessNum < startNum){
            std::cout << "Number too low! Try again!" << std::endl;
            std::cin >> guessNum;
            ++numTries;
        }
        else if (guessNum == startNum) {
            std::cout << "That is correct!" << std::endl;
            ++numTries;
            return numTries;
        }
        else{
            std::cout << "Error: Input does not follow rules!" << std::endl;
        }
    }
    std::cout << "Number of attempts to find the answer: " << numTries << std::endl;
    return 0;
}

2 Likes

Thanks, @markwood! I have one other question does this change things if I try to put that while loop in a function on its own to make the code look cleaner. I tried running my code with another function and I keep getting the wrong number for numTries and a mess up in the printed message once I guess the exact answer.

Here is what it looks like:
Capture

My code looks like this:

#include <iostream>

using namespace std;
int counter(int guessNum, int startNum){
    int numTries = 0;
    while (guessNum != startNum){
        if (guessNum > startNum){
            cout << "Number too high! Try again!" << endl;
            cin >> guessNum;
            ++numTries;
        }
        else if (guessNum < startNum){
            cout << "Number too low! Try again!" << endl;
            cin >> guessNum;
            ++numTries;
        }
        else if (guessNum == startNum) {
            cout << "That is correct!" << endl;
            ++numTries;
            return numTries;
        }
        else if (guessNum > 100 || guessNum < 0) {
            cout << "Error: Input does not follow rules!" << endl;
        }
    }
        return numTries;
    }




int main()
{
    int startNum = 0;
    int guessNum = 0;
    cout << "Welcome to the guessing game! Player 1 enter your number in from 1 - 100" << endl;
    cin >> startNum;
    cout << "Player  2 guess the number Player 1 picked from 1 - 100" << endl;
    cin >> guessNum;
    counter(guessNum, startNum);
    cout << "Number of attempts to find the answer: " << counter(guessNum, startNum) << endl;
}

@Alko89 if you have input as well please share thank you!

@thecil I was told by my tutor you could help as well!

You’re calling the counter function twice. You should only call it once when you print the output (in cout).

You also don’t really need to pass guessNum in the function since you’re only using in inside the function so you can just define it there.

1 Like

Thanks @Alko89 much appreciated!

@Alko89 is right no need to call the func twice!

I found you need to call before cout<< to make presentation in UI make sense.

I made some changes to the While loop to reduce repetition, and also did a forward function declaration, so main() appears first - and learned something my self - use of namespace which came into C++ in 95, showing my age lol

#include <iostream>

using namespace std;

int counter(int startNum);

int main()
{
    int startNum = 0;
    int numTries = 0;
    
    cout << "Welcome to the guessing game! Player 1 enter your number in from 1 - 100" << endl;
    cin >> startNum;

    numTries = counter( startNum);

    cout << "Number of attempts to find the answer: " << guessNum << endl;
    return 0;
}

int counter(int startNum){
    int numTries = 0;
    int guessNum = 0;
    
    while (guessNum != startNum){
        cout << "Player  2 guess the number Player 1 picked from 1 - 100" << endl;
        cin >> guessNum;
    
        if (guessNum > startNum){
            cout << "Number too high! Try again!" << endl;
        }
        else if (guessNum < startNum){
            cout << "Number too low! Try again!" << endl;
        }
        else if (guessNum > 100 || guessNum < 0) {
            cout << "Error: Input does not follow rules!" << endl;
            --numTries;
        }
        ++numTries;
    }
        cout << "That is correct!" << endl;
        return numTries;
    }
1 Like

I know I am :wink: I just don’t want to post the entire code because I want students to figure it out themselves :sweat_smile:

1 Like