Seperation of GetGuessPrintGuess

I split this into two functions.

main.cpp

#include <iostream>
#include <string>

using namespace std;

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

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

void PrintIntro()
{
	// print introduction 
	constexpr int WORD_LENGTH = 5;
	cout << "Welcome to Bill's Bulls and Cows, a fun word game.\n";
	cout << "Can you guess the " << WORD_LENGTH;
	cout << " letter isogram I'm 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++)
	{
		PrintGuess();
		cout << endl;
	}
	return;
}

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

void PrintGuess()
{
	// repeat the guess back to them
	string Guess = GetGuess();
	cout << "Your guess was: " << Guess << endl;
	return;
}

if you are having trouble displaying code in your post, you can find instruction here How to apply code formatting within your post

2 Likes

Privacy & Terms