Preface
I'm going to ignore your teacher's code, at least for the moment, and just review yours.
Based on what you've said, I'm guessing you're still pretty close to the beginning of the learning curve. Based on that, I'm going to go into a little more detail that normal about how to move in a direction I think you'll find beneficial, rather than just talking about where you might like to end up.
Approach
First of all, you currently have all your code in main
. It can be extremely helpful to define small, self-contained functions to carry out the overall task, instead of having it all inline in a single function like this.
To do this, I'd start with a really basic outline of what the program is supposed to do:
- generate a random number
- While they haven't used up their guesses or gotten the right answer:
- get a guess at the random number from the user
- check whether their guess is high, low, or correct
Then I'd consider which of those is easily turned into a separate, self-contained piece of code (hint: most of them).
Then I'd write code in main that worked at pretty much that level, and have it delegate the details to other code. A first stab at it might look something like this:
int main() {
int secretNumber = generate_random();
int maxTries = 3;
for (int guessCount = 0; guessCount < maxTries; guessCount++) {
int guess = get_guess();
if (check_guess(guess, secretNumber))
break;
}
}
Initially, I wouldn't worry a lot about getting every detail precisely correct. Just try to get something that fits reasonably well with the outline you write in English.
From there, you have a couple of choices. One is to start by writing "mock" versions of most of those. They need to do roughly the right sort of thing, but don't put any effort into really making them do the job correctly. For example, we can write a "mock" "generate a random number" as something like:
int generate_random() {
return 7;
}
That obviously won't do in the long term, but it's enough that we can use it to write and test the rest of the code. Then do roughly the same with the other functions:
bool check_guess(int guess, int secretNumber) {
// Todo: add code to print out current result
return guess == secretNumber;
}
int get_guess() {
static int guess;
return guess++;
}
Now we have enough that we can test the basic flow of the program. For example, we can check that when we run it, it doesn't get stuck in a loop; it runs to termination. Once we've established that, we can add enough more to print out each guess, and whether it was right or wrong, and see that as-is, it guesses values for 0 through 10, then quits because it used up the allotted number of guesses. If so, great. If not, we figure out why not and fix that.
Then we change the random number to (say) 5, so it should guess correctly before it runs out of guesses. Then we run that to be sure it does what it should (like, print out the message that you got the right answer, and then quit asking for more guesses once 5
is guessed).
Once we're done verifying that the basic flow of the code in main
works correctly, we can expand out those subordinate functions to do their jobs correctly, so generate_random()
actually generates a random number, get_guess()
really asks the user for input, and do on.
The important point here so to break the large, somewhat complex task down into a number of smaller tasks, each of which is quite simple. This way, it's much easier to define and understand what each piece needs to do, and test the code so we can be sure that it does what it's really supposed to.
Other Points
I think it's worth pointing out that I'm positively impressed with a number of things about your code. You've chosen good, clear variable names, and structured the code so it's really quite clean and understandable. It's longer than your teacher's, but length is rarely a good measure of much of anything, and its length doesn't seem (to me) particularly excessive for what it does. A fair amount of the extra length is simply because your teacher's code is closer to what I've recommended as the first step on the way toward better code--for example, it chooses the same "random" number every time it's run. That's simple and easy to test with, but yours is clearly a more complete program in this regard.
As far as the specific question of whether to use a Boolean variable: it doesn't strike me as necessary in this case, but it's also fairly harmless. I can see writing the code either with or without it, and doubt that either is necessarily much better than the other.
rand()%10
returns a number between 0 and 9 (inclusive). Then(rand()%10)+1
would then return a number between 1 and 10 (inclusive). If you want to a random number between 0 and 10, you need torand()%11
\$\endgroup\$