The user is required to guess a computer-generated number between 1 and 100 inclusive.
User will be told to give a higher or a lower number until the correct number is hit. Display
extra comment such as “You are fantastic!” if the user can hit the number within 3 guesses,
or display “You need more practice!” if the user cannot get the number within 8 guesses.
There is a problem where when the Guess#8 if the user still unable to guess on the 8th tries then the program will promt "You need more practice!".
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
usingnamespace std;
int main(void)
{
srand((unsignedint)time(NULL)); // To not have the same numbers over and over again.
while (true)
{ // Main loop.
// Initialize and allocate.
int number = rand() % 100 + 1; // System number is stored in here.
int guess; // User guess is stored in here.
int tries = 0; // Number of tries is stored here.
cout << "\tWelcome to GUESSING GAME!\n";
cout << "Please enter a number between 1 and 100. Are you up to the challenge?\n";
cout << "-----------------------GUESSING GAME----------------------------------" << endl;
cout << endl;
while (true)
{ // Get user number loop.
// Get number.
cout << "Guess #" << tries + 1 << ":";
cin >> guess;
cin.ignore();
// Check number.
if (guess < number)
{
cout << "No, give a higher number.\n";
cout << endl;
}
elseif (guess > number) {
cout << "No, give a lower number.\n";
cout << endl;
}
elseif (guess == number)
{
cout << "Yes, you are right!" << endl;
break;
}
// If not number, increment tries.
tries++;
}
// Check for tries.
if (tries == 8)
{
cout << "You need more practice !\n";
cout << "The correct answer is : " << number << endl;
break;
}
elseif (tries == 3)
{
cout << "Yes, you are right!";
cout << "You are fantastic!\n";
break;
}
return 0;
}
}
inside the actual while ? because that will solve the problem you have. And what happens if the user guesses it on the first try ? or the second try ? or the fourth try ? you get my point by now because you only check 3rd try and 8th try