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

Directions from book - Write a program that generates a random number and asks t

ID: 3759718 • Letter: D

Question

Directions from book - Write a program that generates a random number and asks the user to guess what the number is. If the user's guess is higher than the random number, the program should display "Too high, try again." If the user's guess is lower than the random number, the program should display "Too low, try again." The program should use a loop that repeats until the user correctly guesses the random number.

Enhance the program so it keeps a count of the number of guesses that the user makes. When the user correctly guesses the random number, the program should display the number of guesses.

You can enhance the program even further by using a switch statement to provide the user with a message indicating their relative skill in guessing the number.

Name the class Lastname04c and save the file as Lastname04c.java. USE YOUR LAST NAME INSTEAD OF THE WORD LASTNAME.

Use the starter file ATTACHED TO THIS ASSIGNMENT to begin this lab (GuessANumber.java)

Use the Scanner class to grab the user's guesses.

Use the Random class to generate a random number between 0-100.

Tell the user that they should guess a number between 1-100.

Use a while loop (since you don't know how many times you need to loop) to:

Increment the guess counter

Tell the user to enter a guess

Have the user enter a guess

Check to see if the guess is right, too low, or too high,

Provide the user with a message indicating if the guess was right, too low, or too high

The loop should be exited when the user guesses correctly.

Use a switch statement to check and see how many guesses it took to obtain the right answer. Provide different messages for 1 guess (amazing!), 2, 3 guesses (expert!), 4,5 guesses (impressive!), 6, 7 guesses (very good!), 8 (try harder) and the default (you're not very good at this)

Not only must your program meet the requirements of the problem,you must also:

Include your name and the program's purpose in a comment that appears before the class declaration.

Follow the naming guidelines for classes.

Your code should be "neat" with the main method indented within the class and the statements in the body of the main method indented within the main method.

You must ATTACH the .java file before submitting the assignment.

Explanation / Answer

//Random Number Guessing Game
#include <iostream>
#include <cstdlib>
#include<ctime>
using namespace std;

int main()
{

int number;
int randomNum;
bool tryAgain= true;
char reply;
int guess;


cout <<"Want to play a game?"<<endl;
cout <<"Try and guess the number I am thinking of"<<endl;
cout <<"and you will win."<<endl;

while (tryAgain)
{

srand(time(0));
randomNum = rand() % 15 + 1;

cout <<"I'm thinking of a number between 1 and 15."<<endl;


cout << " Try and guess my number: "<<endl;
cin>>number;
// what variable do i use to count how many times number != randomNum ??
// i just keep getting a one.
for (int played = 1; played == (number!= randomNum) ; played++)
{
while (number!= randomNum)
{
if (number < randomNum)
{
cout << "Your guess is too low...try again: "<<endl;
cin>>number;
}
if (number > randomNum)
{
cout << "Your guess is too high...try again: "<<endl;
cin>>number;
}

}

if (number == randomNum)
{
cout << "Congratulations you have won."<<endl;
cout << "You made "<<played<<" failed attempts."<<endl;
}
}
cout<<"Dare to try again? Enter y or n: "<<endl;
cin>>reply;
if(reply!='y')
{
tryAgain=false;
cout<<"Quitter."<<endl;
}

}

system("PAUSE");
return 0;
}
[/code]

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