Different method of do and while usage

#include
#include

using namespace std;

//Prototype for the encapsulated abstraction
void PRINTINTRO();
void PLAY_GAME();
string GETGUESS();
bool AskTOPlayAgain();

// This is the entry point of the game
int main()
{
	do
	{
		PRINTINTRO();
		PLAY_GAME();
	}
	while (AskTOPlayAgain()==1);
	return 0;
}

void PRINTINTRO()
{
	// Introduction for the game
	constexpr int WORD_LENGTH = 9;
	cout << "Welcome to Bulls and Cow.A fun word game\n";
	cout << "Can you guess " << WORD_LENGTH;
	cout << " letter isogram I am thinking of?" << endl;
	cout << endl;
	return ;
}

void PLAY_GAME()
{
	// Looping for the no of turns
	constexpr int NO_OF_TURNS = 5;
	for (int i = 1; i <= NO_OF_TURNS; i++)
	{
		string GETGUESS();
		// Print back the word for player
		cout << "Your guess is: " << GETGUESS() << endl;
		cout << endl;
	}
}

string GETGUESS()
{
	// Ask the player to enter a word
	string GUESS = "";
	cout << "Enter your guess: ";
	getline(cin, GUESS);
	cout << endl;
	return GUESS;
}

bool AskTOPlayAgain()
{
	cout << "Do you want to play again?";
	string RESPONSE;
	getline(cin, RESPONSE);
	cout << endl;
	cout << endl;
	return (RESPONSE[0] == 'y') || (RESPONSE[0] == 'Y');
}

I Wrote the Code for DO while in the following way, and it works. wonder what complications would happen ??

Are you talking about while (AskTOPlayAgain()==1);? If so that’s fine as the lecture stated. Also == 1 is redundant

As an aside, you are really inconsistent on your naming. You should pick a naming convention and stick with it, you currently have all pretty much all different naming styles for your functions i.e. PRINTINTRO, PLAY_GAME and AskTOPlayAgain

1 Like

Thank you Dan , I understood , I will make sure of being consistent on naming :slight_smile:

Privacy & Terms