Question

In: Computer Science

1. make a c++ program that generates a random number 1to 100 using rand(). user gets...

1. make a c++ program that generates a random number 1to 100 using rand().
user gets 5 attempts
stop program when they get it right
find out which guess was the closest to the actual number and print out that they have to run the difference in miles for example (50-30)=20 miles
do not use arrays!
very quick upvote for good response

Solutions

Expert Solution

#include <iostream>
using namespace std;
int main()
{
    srand (time(NULL));               // initializing random number generator
    int randomMile;                   //variables for storing random mile value, 
    int minDiffMile = 100;            //minimum random mile value,
    int userInputMile = 0;            //user input mile
    int closestGuess = 0;
    randomMile = 1 + rand() % 100;    //generate a random number 1 to 100
    for (int i = 5; i > 0; --i) {     //loop running 5 times for 5 guesses
        cout << "Guess a number" << '\n';
        cin >> userInputMile;         //taking input
        if (userInputMile == randomMile)    //if user guesses the number right
        {
            cout << "You guessed it RIGHT" << '\n';     //print to display
            return 0;                       //return if user guessed it right
        }
        else{                       //else the guess is wrong
            cout << "You guessed it WRONG, Try left: "+to_string(i-1) << '\n';
            //calculation the difference between random mile and and user input
            int diffMile = (randomMile < userInputMile) ? (userInputMile - randomMile) : (randomMile - userInputMile);
            //storing difference to minDiffMile if it is lesser than previously stored value and storing closest guess
            if (diffMile < minDiffMile)
            {
                closestGuess = userInputMile;
                minDiffMile = diffMile;
            }
        }
    }
    //if user guessed 5 times wrong then program has not returned, hence reached this line
    cout << "Closest guess was: " + to_string(closestGuess) << '\n';    //printing closest guess
    cout << "and difference from closest guess is: " + to_string(minDiffMile) << '\n';  //printing diff
    return 0;
}

CODE SNIPPET

Sample output 1 (When user guessed it right)

Sample output 1 (When user guessed it wrong for 5 times)


Related Solutions

Create a C++ program that generates a random number from 1 to 100 using the rand()...
Create a C++ program that generates a random number from 1 to 100 using the rand() function. Give the user a total 5 chances to guess the number. Stop the program and indicate the user guessed the correct number and should be applauded. Also, please find out which guess was the closest to the actual number. For example, if the rand() produces 57 and the user guessed 10, 80, 52, 33 and 44 in their respective turns then print out...
Write a program that generates a random number between 1 and 100 and asks the user...
Write a program that generates a random number between 1 and 100 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. Your program should also keep a...
Develop a C program that generates a random number from 1 to 100, then prompt the...
Develop a C program that generates a random number from 1 to 100, then prompt the user to guess the number until it is guessed correctly. Let the user know if the guess is too high, too low, or correct. The program should count the number of times the user guesses and display the number, along with their rank, after the number is guessed correctly. Rank the user as follows: Super Guesser: 1 to 4 guesses Excellent Guesser: 5 to...
Write a program that generates a random number in the range of 1 through 100, and...
Write a program that generates a random number in the range of 1 through 100, and asks the user to guess what the number is. When the number is generated by the computer, don’t display the number to the user, but only display if the number generated is odd or even. 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...
Write a program that generates a random number in the range of 1 through 100, and...
Write a program that generates a random number in the range of 1 through 100, and asks the user to guess what the number is. When the number is generated by the computer, don’t display the number to the user, but only display if the number generated is odd or even. 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...
Write a program that generates a random number in the range of 1 through 100, and...
Write a program that generates a random number in the range of 1 through 100, and asks the user to guess what the number is. When the number is generated by the computer, don’t display the number to the user, but only display if the number generated is odd or even. 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...
Random Number Guessing Game Write a program in C++ that generates a random number between 1...
Random Number Guessing Game Write a program in C++ that generates a random number between 1 and 100 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....
In Java: Write a program that generates a random number and asks the user to guess...
In Java: Write a program that generates a random number and asks the user to guess the number and keeps track of how many guesses it took If the user input is negative or zero then the loop must stop reading further inputs and display how many guesses they used If they guess the correct number display a message telling them they got it and exit the program If they guess the wrong number (but still a legal guess) you...
Write a program in C++ coding that generates a random number between 1 and 500 and...
Write a program in C++ coding that generates a random number between 1 and 500 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. Count the number...
17. Write a program that generates a random number and asks the user to guess what...
17. 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. 18. Enhance the program that you wrote for Programming...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT