Using C++ microsoft visual studios 2013 write a program (with //comments) with a
ID: 3824805 • Letter: U
Question
Using C++ microsoft visual studios 2013 write a program (with //comments) with a method that plays the guess a number game. The program should allow the user to pick a number between 1 and 1000 in his head. The method should guess the user's number in a minimal amount of attempts. The method should ask the user "is the number greater than or less than the number " and the program gives a particular number. The user in some way just inputs (higher or lower) or (greater than or less than). There also has to be a way for the user to say the number has just been guessed. Of course, the user cannot answer incorrectly or change the number midstream. Note - if written efficiently, the method should be able to guess any number in 10 or less attempts.
Explanation / Answer
Here is the C++ code for the above scenario:
#include <iostream>
#include<time.h>
using namespace std;
int main ()
{
int random;
int num;
int max;
int min;
int outofrange;
int rightguess;
rightguess = 1;
outofrange = 0;
max = 1000;
min = 1;
srand(time(NULL));
cout << "random number: " << rand()%10 << endl;
cout << "Enter an interger that is between 0 and 100: "<< endl;
cin >> num;
while (num != rand()%10)
{
if ( num > rand()%10)
{
cout << num << " is too high" << endl;
max = num - 1;
}
else
{
cout << num << " is too low" << endl;
min = num + 1;
}
cout << "Enter an interger that is between " << max << " and "<< min << endl;
cin >> num;
if (num > max || num < min)
{
cout << "the interger you enter is not within range, enter a number between " << max << " and " << min << endl;
cin >> num;
outofrange = outofrange + 1;
}
else
rightguess = rightguess +1;
}
cout << " It took you " << rightguess << " valid guesses to find the number " << endl;
cout << " You had " << outofrange << " out of range guess " << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.