My For Loop Code Approach

I would have put this into a topic for everyone’s completion of this, but there doesn’t appear to be one. As such, I’ll include the relevant code I used below. Screenshots are a bit of a bother for me when I can just as easily copy and paste the relevant code into the discussion. Here is my for loop solution:

for (int count = 5; count != 0; count–)
{
GetGuessAndPrintBack();
}

I chose this solution to do away with the Magic Number issue without the need of creating another variable. By doing this, count becomes the number that will need to be modified. Zero is not a magic number and altering count will always work for varying how many guesses are alloted.

I’m not sure if this was an efficiently clever solution, or if I’ve made some sort of error in judgment without realising it. Either way, this was my approach to the challenge! I hope it was okay to create a new topic for this. I saw that others had…

~Julia

Thats cool Smulia, I’m a little past this part and was too much of a wuss to post my solution but I remember it was somewhat similar. I like the way you approached it but I’m a complete noob when it comes to coding so I don’t know if there is a better way to do things.

Well, I did think of a few things that could be bad about it. the variable count has a very limited scope with it being declared in a for loop. I don’t think it’s scope can go outside it from what I’ve read. If this is the case, you could use a second variable to reference it. I went forward in the video and saw where a new constexpr was used. I adapted this to my own purposes in case I needed that variable later on outside of the for loop. This is the code now:

// loop for the number of turns asking for guesses
constexpr int NUMBER_OF_TURNS = 5;
for (int count = NUMBER_OF_TURNS; count != 0; count--)
{
	GetGuessAndPrintBack();
	cout << endl;
}

Now NUMBER_OF_TURNS can be used anywhere in the main function! :smiley: :smiley: :smiley:

~Julia