i need help writing a modified version of the Magic Number program. For this program, the user is asked to think a number between 1 and 10, and the computer tries to (randomly) “guess” the number. The user should provide clues to the computer so that the random number the computer generates keeps getting closer to the answer in each step. The following is a sample run of the program:
Think of a number between 1 and 10. Is 3 the number you are thinking of? Enter h if the number you are thinking of is higher, l if it is lower or c if it is correct.
h
Is 5 the number you are thinking of? Enter h if the number you are thinking of is higher, l if it is lower or c if it is correct.
h
Is 9 the number you are thinking of? Enter h if the number you are thinking of is higher, l if it is lower or c if it is correct.
c
I guessed your number in 3 steps.
this is how it should it up after being complied and ran. please need help anything will be considered valuable, thank you.
//Magic Number
//Created/revised by <Miguel Mora> on <11-28-13>
#include <cstdlib> //needed for rand() and srand()
#include <ctime> //needed for time()
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int max = 10, min = 1;
srand(time(0)); //seed the random number generator
int r = (rand() % 10) + 1;
int numberOfSteps = 0;//counter
int steps = 0;
int letterH = 0;
int letterL = 0;
int letterC = 0;
//initialize rand function
srand(static_cast<int>(time(0)));
//enter number
cout << "Think of a number between 1 and 10. Is 2 the number you are thinking of? Enter h if the number you are thinking of is higher, l if it is lower or c if it is correct.: ";
cin >> letterH;
char ans;
do
{
//update counter and
numberOfSteps += 1;
//get remaining times
cin >> letterH;
} //end while
while (letterC != 'c');
if (letterH
//vertify that at least one number was entered
if (letterC > 0)
{
//calculate and display number of steps
steps = (numberOfSteps);
cout << fixed << setprecision(1);
cout << endl << "I guessed the your number in steps: "
<< steps << endl;
}
else
cout << "No letter was entered."<< endl;
//end if
cout<< endl;
system("pause");
return 0;
} //end of main function
srand(static_cast<int>(time(0))); this is not necessary you already seeded it. cout << "Think of a number between 1 and 10. Is 2 the number you are thinking of? Enter h if the number you are thinking of is higher, l if it is lower or c if it is correct.: "; why do you ask if 2 is lower or higher? Shouldn't you use the number you randomly generated? int r = (rand() % 10) + 1;
Your code I am not sure on the logic behind it but basically it should look something like this:
1) Generate a random seed
2) Ask the user to enter a number between min(1) and max(10).
3) Do/while loop - ( loop while user does not enter 'c' -- correct )
4) Generate a random number -- rand() % ( max - min + 1 ) + min.
5) Ask user if the random number was higher ('h') , lower('l') , or correct('c')
6) if user enters 'c' go to step 10.
7) if user enters 'h' set min = guess
8) if user enters 'l' set max = guess
9) increment number of steps and return to step 4.
10) output how many steps it took to guess the correct number.