Clarity diffrent approach?

Hi,

I was following the course and came up with the following solution:

// repeat input to the player
void PlayGame() 
{
	constexpr int NUMBER_OF_TURNS = 5;
	for (int count = 1; count <= NUMBER_OF_TURNS; count++) 
	{
		cout << "The word you typed was: " << GetGuess() << endl << endl;
	}
}

I’m calling the function GetGuess() while printing instead of declaring it to Guess like in the video. Is this also a right way of doing it, or will this be too confusing to read?

I think it’s fine if the only thing you’re doing is printing it out. Once you move on to doing other things with the guess, such as verifying if the guess is valid, you’ll need to move it.

But you understand what’s happening, and can see how things can be rewritten to have the same meaning, and that’s good.

If you’re GetGuess function has an output as well like Ben does “Enter your guess:” then the output of this is unspecified behaviour

cout << "The word you typed was: " << GetGuess() << endl << endl;

Your output could be

"The word you typed was:  Enter your guess: "

and that would be perfectly valid. In C++17 the order of evaluation of that statement is guaranteed to be the above.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms