I need my code to display certain messages after it states you won depending on how many guesses it took the user to get it right.
guesses < 4 "Either you know my secret or you got lucky"
guesses >= 5 || guesses <= 7 "You're pretty good at this"
guesses >= 8 || guesses <= 10 "You'll do better next time"
If anyone could help I would appreciate it. Thank you!
//This program generates a random number between 1 and 100.
//The user gets to try and guess the number.
//Will you guess the number?
#include <iostream>
#include <time.h>
#include <string>
usingnamespace std;
int main()
{
int guesses = 10, guess, answer;
srand(time(NULL));
answer = rand() % 100 + 1;
cout << "Ready to play the guessing game?\n";
cout << "Can you guess the number I'm thinking of between 1 and 100, before you\nrun out of guesses?\n\n";
for (int i = 0; i < guesses; i++)
{
cout << "Guess the number I am thinking: ";
cin >> guess;
if (guess < 1 || guess > 100)
cout << "Invaild guess please pick a number 1-100.\n";
elseif (guess != answer)
{
if ( guess > answer)
cout << "To high! try again.\n";
else
cout << "To low! try again.\n";
}
else
{
cout << "You won!!\n";
}
}
cout << "Sorry - you have taken to many guesses";
system("pause");
return.0;
}
The first thing you need to do is break out of the loop when the correct guess is made, Then if your loop counter (i) was declared before the loop you could use that variable to tell how many guesses they made before they got it correct.