Is this bad practice for do while?

instead of creating a new variable i tried putting my function call directly in the while check and it worked fine. is there a reason not to do this??

while (AskToPlayAgain());

I’ve used a similar solution, so I am going to post it here:
do
{
PrintIntro();
PlayGame();
}
while (AskToPlayAgain() == true);

this worked fine. My Assumption was to skip the use of an extra variable.

Like Stephen, I am curious as to whether this is a worst practice in comparison to the one shown in the course.

Best

1 Like

While it’s probably not a “Worst Practice”, there are things to consider when using this type of shortcut.

Consider a scenario where there is a limited number of plays… like all the FTP games that give you so many free plays in a given time period so they can charge you a premium to exceed those number of plays… If you wanted to add that functionality to the example, it’s just a matter of something like

WantsToPlayAgain=false;
if (HasTurnsRemaining()) WantsToPlayAgain=AskToPlayAgain();
while(WantsToPlayAgain)

That being said, as I was typing that example I realized that you could put the HasTurnsRemaining() check within AskToPlayAgain…
In any event, it’s a matter of looking at future functionality and ease of code reading. Does it pass the “year” test? (All code should be written in a way that you could walk away from it for a year, come back, and see what’s going on). Would a new programmer to the project be able to see what you were intending? I’d say in both cases, the answer would probably be “yes”.

1 Like

So I tried this as well @Stephen_Frey, and it works until I put in a answer that doenst have y or Y. If I set it to false, it still restarts the game instead of ending. If I do it exactly as you did, I get asked twice to play again or not. Any help would be appreciated

#include <iostream>
#include <string>
using namespace std;

void PrintIntro();
void PlayGame();

string GetGuess();
string Response();
bool AskToPlayAgain();

// The entry point for our application
int main()
{
	//bool bPlayAgain = false;
	do
	{
		PrintIntro();
		PlayGame();
		AskToPlayAgain();
	} while (AskToPlayAgain);
		return 0; // exit the application
}

// introduce the game
void PrintIntro()
{
	constexpr int WORD_LENGTH = 5;
	cout << "Welcome To Bulls and Cows, A Fun Word Game\n";
	cout << "Can you guess the " << WORD_LENGTH;
	cout << " letter isogram I am thinking of?\n";
	cout << endl;
	return;
}

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

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

bool AskToPlayAgain()
{
	cout << "Do you want to play again, Yes or No? ";
	string Response = "";
	getline(cin, Response);

	(Response[0] == 'y' || 'Y');
	cout << endl;
	return (Response[0] == 'y') || (Response[0] == 'Y');
}
1 Like
do
{
	PrintIntro();
	PlayGame();
    AskToPlayAgain();
} while (AskToPlayAgain);
  1. You are attempting to call AskToPlayAgain twice

  2. I say attempting because you are missing () in the while loop condition
    The code should look like

    do
    {
    PrintIntro();
    PlayGame();
    } while (AskToPlayAgain())

1 Like

Awesome Thank you very much bud!

When you are writing (Response[0] == ‘y’ || ‘Y’); you are setting the first char to y. So no matter what you write in the input it will always come out to y and restart the game. Comment that line out with // and that bug should be fixed buddy.

This is what I did, and I was curious enough to see what everyone else thought, looks like Im not the only one. Im glad I figured it out though :slight_smile:

1 Like

Using the

while (AskToPlayAgain() )

format seemed the most natural way of doing this also for me.

Privacy & Terms