First For Loop (no magic numbers!)

Here’s how I did my first For Loop and avoided magic numbers:

#include <iostream>
#include <string>

using namespace std;

void PrintIntro();
string GetGuessAndPrintBack();

// entry point for our application
int main()
{	
	PrintIntro();

	int NumberOfGuesses = 4; // create variable NumberOfGuesses

	for (int count = 0; count <= NumberOfGuesses; count++) // Count from 0 to NumberOfGuesses
	{
		GetGuessAndPrintBack();
	}

	return 0;
}

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

// get a guess from the player
string GetGuessAndPrintBack()
{
	string Guess = "";
	cout << "Make a guess: ";
	getline(cin, Guess);
	
	// repeat the guess back to them
	cout << "Your guess was: " << Guess << endl;
	return Guess;
}
1 Like

Privacy & Terms