#include <cstdlib>
#include <iostream>
usingnamespace std;
int main(int argc, char *argv[])
{
srand(time(0));
cout << "** WELCOME TO AI NUMBER GUESS **\n\n";
cout << "Think of a number in your head between 1 and 100.\n\n";
cout << "Once you're ready, press any key and I will guess the number you chose.\n\n";
cout << "Then you will have to tell me whether the number you chose is lower\n\n";
cout << "or higher than the number I guessed.\n\n";
int guess;
int upperBound;
int lowerBound;
guess = ?;
upperBound = 100;
lowerBound = 1;
cout << "Once you have chosen your number, press any key.\n\n";
char a;
cin >> a;
char c;
cin >> c;
}
Now, I want to limit the generation of the first guess to a number between 1 and 100. I already seeded the random number generator at the beginning of the code, but how do i limit to be 1 =< guess =< 100.
In other words, what should guess = ?
And then, using the switch statement, can tell me if i'm right about this way of coding it?
This will create a random number between lowerBound and upperBound.
The switch statement is not valid. You must not have an expression in a case. You have to do it with if-else-statements, but i don't really get the aim of your switch-case-statement anyhow. Can you explain, what you are trying to do?
Thanks for the equation, that's what I was looking for, but it doesn't give me a number between 1 and 100. Actually, it's been generating numbers over 200,000...What's going on? lol
1 2 3 4 5 6 7 8 9 10 11 12
cout << "Once you have chosen your number, press any key.\n\n";
char a;
cin >> a;
char yes = 'y';
while (yes == 'y')
{
cout << "Is your number " << guess << " ? (y/n)" << endl;
cin >> yes;
cout << "See, I can read your mind!\n\n";
}
Basically, forget about the switch-case-statement.
I was hesitating between that and if-else-statements.
The program should guess the number you are thinking of as a user. So basically you think of a number, then the console app generates a number and asks you if the number it guessed is the number you thought of or not.
Then if yes, its over with. If no, then the console app asks the user if the number it guessed is higher or lower than the number the user had in mind. Depending on the case, the console then narrows it down and guesses a new number in a new range and so forth until it finds the right number.
char yes = 'n';
while(yes != 'y'){
calculate guess;
Ask if guess is higher, lower or correct
if higher
set lowerBound to new value
elseif lower
set upperBound to new value
}
cout << "I read your mind!" << endl;
char relation;
do{
cout << "Enter 'h' if your number is higher and 'l' if lower/n/n";
cin >> relation;
if (n == 'h')//player said their number is higher
{lowerBound = guess+1; break}
elseif (n == 'l')//player said their number is lower
{higherBound = guess-1; break}
}
"cout << "I read your mind!" << endl;" - quite cheesy considering it could take as many as 100 guesses to get it right lol