Reila01 - this sounds like this guessing game could be homework. I think that you have the correct pieces of the code, just not necessarily all in the correct place. For example, if the user incorretly guesses the secret number, then you need to prompt him or her to try again. This suggests that the input prompt should be inside your while loop. The condition
if numguesses == secretnumber
is incorrect, since you are not comparing the number of guesses with the secret number, but the guess (the result from the input prompt) with the secrent number. Further if this is true, then you can compare the numguesses with 3 - if 3 or less, then display the "correct!" message. If it took more than three guesses, then display the "correct but you still lose" message. As the user has guessed the answer (for how many guesses it took), then you will want to break out of the while loop since (though I guess the condition on the while loop will take care of this). Also, if you want to print the number of guesses that it took
fprintf('it took you %g guesses. You have won the game!');
you need to include the variable as a parameter input to fprintf (and use %d for an integer output)
fprintf('it took you %d guesses. You have won the game!', numguesses);
Best Answer