Number guessing Game INVERTED - PROBLEM

Hello guys,

Alright, it's tedious to explain what the little console application I want to make does, so I'll just show you the code. It explains it.

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
#include <cstdlib>
#include <iostream>

using namespace 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?
1
2
3
4
5
6
7
8
9
10
11
12
13
 cin >> lower;
switch (lower);
{
case (guess < 50):
    ...
    ...
    break;
case (guess < 25):
    ...
    ...
    break;
    ..................
)


I'm really not sure about the switch statement part.

Anyway, please help me out!
Thank you!
Last edited on
I think you are looking for something like

guess = rand() % (upperBound - lowerBound + 1) + lowerBound

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.

Think you can help me?

Last edited on
Bump...Kinda need help really bad.
I don't know how to structure this.
You can do it like this:

1
2
3
4
5
6
7
8
9
10
char yes = 'n';
while(yes != 'y'){
 calculate guess;
 Ask if guess is higher, lower or correct
 if higher
  set lowerBound to new value
 else if lower
  set upperBound to new value
}
cout << "I read your mind!" << endl;
This is just an example and not actual code, right?
yes...


after you calculate the guess

1
2
3
4
5
6
7
8
9
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}
else if (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
Topic archived. No new replies allowed.