Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

c++ // CHANGE #1: // THE ONLY CHANGE IS THAT I WANT YOU TO LET THEM HAVE // UNLI

ID: 3719891 • Letter: C

Question

c++

// CHANGE #1:

// THE ONLY CHANGE IS THAT I WANT YOU TO LET THEM HAVE

// UNLIMITED GUESSES AND NOW YOU KEEP

// TRACK OF HOW MANY GUESSES IT TAKES AND THEN

// PRINT OUT "You took X tries to guess the number.".

// HI LO Game
#include <iostream>
#include <cstdlib>   //STEP 1 out of 4 for random numbers
#include <ctime>     //STEP 2 out of 4 for random numbers
using namespace std;
int main() {
int num;             //Variable to store the random number
int guess; //variable to store the number guessed by the user
int i;

srand(time(0));      //STEP 3 out of 4 for random numbers
num = rand() % 100; //STEP 4 out of 4 for random numbers

for (i=1; i<=10; ++i)
{

cout << endl;
cout << "Enter an integer greater" << " than or equal to 0 and " << "less than 100: ";
cin >> guess;

if (guess == num) {
   cout << "You guessed it in " << i << " tries!" << endl;
   break;
}
else if (guess < num)
   cout << "TOO LOW - Your guess is lower than the " << "number. Guess again!" << endl;
else
   cout << "TOO HIGH - Your guess is higher than " << "the number. Guess again!" << endl;

} //end for

system("pause");
return 0;
}

Explanation / Answer

#include <iostream>
#include <cstdlib>   //STEP 1 out of 4 for random numbers
#include <ctime>     //STEP 2 out of 4 for random numbers
using namespace std;

int main() {
int num;             //Variable to store the random number
int guess; //variable to store the number guessed by the user
int count = 0;

srand(time(0));      //STEP 3 out of 4 for random numbers
num = rand() % 100; //STEP 4 out of 4 for random numbers

do //loop until num is equal to guess
{

cout << endl;
cout << "Enter an integer greater" << " than or equal to 0 and " << "less than 100: ";
cin >> guess;
count++; // increase the guess counter
if (guess == num) {
   cout << " You took "<<count<<" tries to guess the number." << endl;
   exit(0);
}
else if (guess < num)
   cout << "TOO LOW - Your guess is lower than the " << "number. Guess again!" << endl;
else
   cout << "TOO HIGH - Your guess is higher than " << "the number. Guess again!" << endl;

} while(guess != num);

system("pause");
return 0;
}

Output:

Enter an integer greater than or equal to 0 and less than 100:23 TOO LOW - Your guess is lower than the number. Guess again!

Enter an integer greater than or equal to 0 and less than 100:90 TOO HIGH - Your guess is higher than the number. Guess again!



Enter an integer greater than or equal to 0 and less than 100: 45TOO LOW - Your guess is lower than the number. Guess again!

Enter an integer greater than or equal to 0 and less than 100: 18TOO LOW - Your guess is lower than the number. Guess again!

Enter an integer greater than or equal to 0 and less than 100: 29TOO LOW - Your guess is lower than the number. Guess again!

Enter an integer greater than or equal to 0 and less than 100: 30TOO LOW - Your guess is lower than the number. Guess again!

Enter an integer greater than or equal to 0 and less than 100: 38 TOO LOW - Your guess is lower than the number. Guess again!

Enter an integer greater than or equal to 0 and less than 100: 87 TOO HIGH - Your guess is higher than the number. Guess again!



Enter an integer greater than or equal to 0 and less than 100: 34TOO LOW - Your guess is lower than the number. Guess again!

Enter an integer greater than or equal to 0 and less than 100:29 TOO LOW - Your guess is lower than the number. Guess again!

Enter an integer greater than or equal to 0 and less than 100: 31TOO LOW - Your guess is lower than the number. Guess again!

Enter an integer greater than or equal to 0 and less than 100: 19 TOO LOW - Your guess is lower than the number. Guess again!

Enter an integer greater than or equal to 0 and less than 100: 28 TOO LOW - Your guess is lower than the number. Guess again!

Enter an integer greater than or equal to 0 and less than 100:21 TOO LOW - Your guess is lower than the number. Guess again!

Enter an integer greater than or equal to 0 and less than 100: 31 TOO LOW - Your guess is lower than the number. Guess again!

Enter an integer greater than or equal to 0 and less than 100: 26 TOO LOW - Your guess is lower than the number. Guess again!

Enter an integer greater than or equal to 0 and less than 100: 71TOO HIGH - Your guess is higher than the number. Guess again!



Enter an integer greater than or equal to 0 and less than 100: 10 TOO LOW - Your guess is lower than the number. Guess again!

Enter an integer greater than or equal to 0 and less than 100: 6 TOO LOW - Your guess is lower than the number. Guess again!

Enter an integer greater than or equal to 0 and less than 100: 14 TOO LOW - Your guess is lower than the number. Guess again!

Enter an integer greater than or equal to 0 and less than 100: 29TOO LOW - Your guess is lower than the number. Guess again!

Enter an integer greater than or equal to 0 and less than 100: 27 TOO LOW - Your guess is lower than the number. Guess again!

Enter an integer greater than or equal to 0 and less than 100: 10 TOO LOW - Your guess is lower than the number. Guess again!

Enter an integer greater than or equal to 0 and less than 100:14 TOO LOW - Your guess is lower than the number. Guess again!

Enter an integer greater than or equal to 0 and less than 100: 35

You took 25 tries to guess the number.

Do ask if any doubt. Please upvote.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote