Hey guys! New programmer here, been teaching myself with a few different beginner/basic books. So this code/project is not for a grade nor will it be used for anything serious. Just a simple test one of the books has given me that i could use some helpful advice, hints or answers if anyone is up to it.
My task is to write a program that guesses a user's number (1-100) using a random number generator.
My approach was to first state the rules to the user saying that the computer will guess a number, then based on that number the user would respond with either h or l for higher or lower, and my program would adjust the next guess based on the users input.
my trouble comes when trying to make the random n
umber generator change its range (from 1-100 to 1-X based on the first randnum generated)
sorry for my lengthy post, here is what i have so far
Thanks in advance!
P.S - please remember im a beginner =(
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
srand(static_cast<unsigned int>(time(0)));
int randNum = rand() % 100 + 1;
char ready = n;
char input;
cout << "This program will guess your number from\n"
<< "anywhere between one and one hundred.\n\n"
<< "When the computer guesses your number please\n"
<< "respond accordingly to the computer's guess\n"
<< "H - higher (if the guess was below your number)\n"
<< "L - lower (if the guess was higher than your number\n"
<< "Please think of your number now. . .\n\n"
<< "When you are ready type y";
cin >> ready
>> endl;
while (ready == y)
{
cout << "Computer 'I think your number is: '"
<< randNum
cin >> input
switch (input)
{
case h:
randNum =
cout << "Well then your number must surely be: "
<< randNum
}
}
| |
EDIT: the way i used to generate the random number was described in the book i am using, i am stuck where my code stops, what i want to do next is take the current random number, then based on user input, adjust the range of the next number to be generated, either higher or lower.
(if i wasn't clear enough before)