Challenge failed: The game keeps asking me twice for replay

Hello,

I did the challenge in lecture 25: Using do and while in c++.
Now I got it working up to a point that it indeed will go ahead and restart the game after I enter “y”.
However, it asks me twice. I cant for the life of me figure out why its asking me twice.

Regards, Kim.

Show us your AskToPlayAgain() function.

Hello,

thank you for your reply! Here is the rest of the code with the AskToPlayAgain() function.

Regards, Kim

That looks fine. Try Rebuilding the Solution under the Build tab and then rerunning. It may be that your commented out call to AskToPlay() under PlayGame() was not commented out in your last build.

Although, it would be best to just remove the commented out code and let the compiler prompt you to rebuild. It is actually poor practice to have lines of commented out code in your program.

My program was displaying the same behavior and I tracked the bug down to the fact that it was running the PlayAgain() twice. I recommend looking through your code to see if it might be doing the same

@Incantrix I did a rebuild but that didn’t solve the issue. Below is a screenshot of this.

@Capricas_Kirito I’m was already guessing it did that, but no clue where too look within my code. It all seems ok… I’ll try looking again just to make 100% sure. In the screenshots above I have shown my code when it comes to AskToPlayAgain(), maybe you can see the fault?

Thank you both for the reply! :smile:

Sorry, based on what you’ve posted I’m unable to see a duplicate call

Here is my complete code:

#include <iostream>
#include <string>

using namespace std;

void PrintIntro();
void PlayGame();
string GetGuess();
bool AskToPlayAgain();


// the entry point for our application
int main()
{ 
	do
	{
		PrintIntro();
		PlayGame();
		AskToPlayAgain();
		
	} while (AskToPlayAgain());

	
	return 0; // exit the application

}


// introduce the game
void PrintIntro()
{
	constexpr int WORLD_LENGTH = 9;
	cout << "Welcome to Bulls and Cows, a fun word game.\n";
	cout << "Can you guess the " << WORLD_LENGTH;
	cout << " letter isogram I'm thinking of?\n";
	cout << endl;
	return;
}



// loop for the number of turns asking for guesses
void PlayGame()
{	
	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;
	}
}

// get a guess from the player
string GetGuess()
{
	cout << "Enter your guess: ";
	string Guess = "";
	getline(cin, Guess);
	return Guess;
}

//Ask the player to play again, yes or no?
bool AskToPlayAgain()
{
	cout << "Do you want to play again y/n?";
	string Response = "";
	getline(cin, Response);

	return (Response[0] == 'y') || (Response[0] == 'Y');
}

Yes, that is not the exact code you first posted. In the first screenshot you had AskToPlayAgain() commented out.

Just remove the AskToPlayAgain() from underneath PlayGame().

Lol Don’t make us work harder Kim.

LOL, you’ve done the same thing as me. Your program prints the intro then runs the game loop and then checks from inside the do/while loop to see if you want to play again. Then it exits the loop and executes the while command

This was literally the first thing I tried when I discovered the problem!
But I removed it completely and now its working fine.

The reasons the code changed a bit is because I tried different things myself in the meanwhile haha.
Sorry to make you work, and thank you for the replies! Much appreciated.

See you in the next post :joy::sweat_smile:

Regards, Kim

1 Like

Privacy & Terms