My code for taking 5 guesses...Bonus use of "magic numbers"

//get 5 guesses from player
string GetGuessAndPrintBack() {
	string Guess = "";
	for (int GuessCounter = 1; GuessCounter < 6; GuessCounter++) {
		cout << "Please enter guess number " << GuessCounter << ": ";
		getline(cin, Guess);
		cout << "Your guess is: " << Guess << endl;
		cout << endl;
	}
	return Guess;
}

I see that I completely missed the point of magic numbers, as well as using the for loop.

It works now as it is in this stage of the project but most assuredly I think I’ll be running into walls later on down the line…

hmmm to leave it as is or change it when it needs to be changed? It all depends on how well I know my code and how it will execute.

I would definetely recommend to put your number into a variable. It woun’t take long and you’ll appriciate it as you read your code. :slight_smile:

Do you have an example line of code for me, please?

Instead of just saying GuessCounter < 6 in your for loop, at the top of your function have something like const int NUM_GUESSES = 5 and replace the for loop’s conditional with GuessCounter <= NUM_GUESSES. Makes the code easier to understand and modify later, and I would recommend the <= instead of just < so that the variable’s value is the actual number of times the loop will run, since you’re starting GuessCounter at 1.

1 Like

Privacy & Terms