Question
In this warm-up project, you are to write a C++ program LabProj1.cpp that lets the user or the computer play a guessing game. The computer
In this warm-up project, you are to write a C++ program LabProj1.cpp that lets the user or the computer play a guessing game. The computer randomly picks a number between 100 and 200 inclusively, and then for each guess the user or computer makes, the computer informs the user whether the guess was too high or too low. The program should end when selected the correct number. Sample Output:
Would you like to (p)play or watch the (c)computer play or (q)quit?
c
The computer's guess is 150
Sorry, your guess is too low, try again.
The computer's guess is 175
Sorry, your guess is too low, try again.
The computer's guess is 187
Sorry, your guess is too low, try again.
The computer's guess is 193
Sorry, your guess is too high, try again.
The computer's guess is 190
Sorry, your guess is too low, try again.
The computer's guess is 191
Sorry, your guess is too low, try again.
The computer's guess is 192
Congrats, you guessed the correct number, 192.
Would you like to (p)play or watch the (c)computer play or (q)quit?
q
MY CODE WILL NOT SUCESSFULLY GUESS THE RIGHT NUMBER FOR THE COMPUTER SECTION..... THE USER PORTION WORKS PERFECTLY. THE COMPUTER JUST SAYS HIGH OR LOW NEVER FINDS THE CORRECT ANSER.
#include#include #include using namespace std; int main() { srand(time(0)); char choice; int random = rand() % 101 + 100; do { cout << "Would you like to (p)play or watch the (c)computer play? "; cin >> choice; int guess; if (choice == 'p') { do { cout << "Enter your guess between 100 and 200. "; cin >> guess; if (guess > random) { cout << "Sorry, your guess is too high, try again. "; } else if (guess < random) { cout << "Sorry, your guess is too low, try again. "; } else { cout << "Congrats, you guessed the correct number, " << random << ". "; } } while (guess != random); } else if (choice == 'c') { int num = rand() % 101 + 100; do { cout << "The computer's guess is " << num << endl; if (num < random) { cout << "Sorry, your guess is too low, try again! "; num += rand() % (random - num) + 1 ; continue; } else if (num > random) { cout << "Sorry, your guess is too high, try again! "; num -= rand() % (num - random) + 1; continue; } else if (num = random) { cout << "Congrats, you guessed the correct number," << random << endl; } } while (num != random); } } while (choice != 'q'); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started