The code compiles successfully but behaves weirdly, need help

I created a function to print back the guess entered bu the player and called it PrintBack() , after i click Ctrl + F5 the code compiles successfully with no errors, but it asks me to enter my guess twice before printing the result. So all in all i have to enter 10 guesses instead of five as cleared by the screenshot.

.
This is my code:


#include
#include

using namespace std;

void PrintIntro();
void PlayGame();
string GetGuess();
string PrintBack();

// entry point for application
int main()
{
PrintIntro();
PlayGame();
return 0; // exit the application
}

// loop for the number of turns asking for guesses
void PlayGame()
{
constexpr int MAX_NUMBER_OF_TURNS = 5;
for (int Count = 1; Count <= MAX_NUMBER_OF_TURNS; Count++)
{
GetGuess();
PrintBack();
}
}

// introducing the game
void PrintIntro()
{
constexpr int WORD_LENGTH = 6;
cout << “Welcome to Bulls and Cows! A fun word guessing game.\n”;
cout << “Can you guess the " << WORD_LENGTH << " letter isogram i’m thinking of?\n”;
cout << endl;
return;
}

// getting input from the player and then returning it back to them
string GetGuess()
{
string Guess = “”;
cout << "Enter your guess: ";
getline(cin, Guess);
return Guess;
}

// print back what the player has entered
string PrintBack()
{
string Guess = GetGuess();
cout << "You entered: " << Guess << endl;
cout << endl;
return Guess;
}


Thank you.

Use 3 backticks around your code for code formatting
```
code goes here
```

Anyway your problem is that in PlayGame in the for loop you are calling GetGuess but you are also calling GetGuess in the PrintBack function.

Thank for the reply and the instructions, it’s my first post here.
How am i calling GetGuess in the PrintBack ?
The GetGuess(); is giving the value to Guess, I don’t see where i’m calling it…

It’s getting a value by calling that function

// print back what the player has entered
string PrintBack()
{
	string Guess = GetGuess(); // <- right here
	cout << "You entered: " << Guess << endl;
	cout << endl;
	return Guess;
}

Breaking down the execution, first in the for-loop you call GetGuess which asks for a guess and will return a string, then don’t do anything with that returned value then you call PrintBack, which also calls GetGuess and you give that returned value to Guess, you log it out to the console and then return Guess to the caller and again, do nothing with the returned value.

But Ben did the same thing in the lecture. Maybe i’m wrong. Anyways, how should i call it then?

Check my edit, there a numerous ways to solve it. Simplest is to just remove GetGuess from the for loop. And looking at the code from the lecture that’s tagged in this post. There is no PrintBack function. The for loop is just

void PlayGame()
{
    // loop for the number of turns asking for guesses
    constexpr int NUMBER_OF_TURNS = 5;
    for (int count = 1; count <= NUMBER_OF_TURNS; count++) {
        string Guess = GetGuess();
	cout << "Your guess was: " << Guess << endl;
	cout << endl;
    }
}

Well i wanted to make a function for this, and i saw numerous people doing it. Anyways it looks like they have more experience than i do.
I’ll just go with what i know for now, and see maybe i’ll learn how to do it in a future lecture.
Thank you for your time.

Well you were almost there but it seems like you don’t fully understand how function calls and return values work and how to use them just yet.

I did the same thing you did by using a PrintBack function but i defined my variable string Guess at the top of my code so i had no need to call GetGuess outside of the for loop

Don’t know if you still have the problem but this may help if you didn’t manage to correct it :slight_smile:

That’s is what you call a global variable, a variable that’s available to all functions in that file and is generally a bad idea, and you would usually have to have a good reason to do that. You would normally pass variables as arguments into other functions like


{
    //code
    std::string Guess = GetGuess();
    PrintBack(Guess);
    //code
}

void PrintBack(std::string Guess)
{
    std::cout  << Guess << std::endl;
}

Privacy & Terms