I need the following to tell how many tries it took and also only allow 7 guesses? #include #include

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899604
Joined: Mon Aug 02, 2021 8:13 am

I need the following to tell how many tries it took and also only allow 7 guesses? #include #include

Post by answerhappygod »

I need the following to tell how many tries it took and also
only allow 7 guesses?
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
srand(static_cast<unsigned
int>(time(0)));
int compNumber = rand() % 100+1;
int low = 1;
int high = 100;
int yourNumber;
do
{
yourNumber = 0;
cout << "Please input a number
between 1 and 100" << endl;
cin >> yourNumber;
} while (yourNumber < 1 || yourNumber>100);
while (compNumber != yourNumber)
{
if (yourNumber < compNumber)
{
cout << "I guessed
" << compNumber << " (too high)" << endl;
high = compNumber;
compNumber = (compNumber
+ low - 1) / 2;
}
else
{
cout << "I guessed
" << compNumber << " (too low)" << endl;
low = compNumber;
compNumber = (compNumber
+ high + 1) / 2;
}
}
cout << "I guessed it! Your number is "
<< compNumber << endl;
return 0;
}
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply