A fun guessing game

The directions are simple:
Write a program that calculates a random number 1 through 100. The program will guess the number. If the computer guesses too high or too low then the user should input "too high" or "too low" accordingly. The user must let the program continue to guess until the user correctly guesses the number. [Modify the program so that no matter what number the user thinks of (1-100) the computer can guess it in 7 or less guesses.]

I'm just having trouble with this switch statement. It won't read it, or it is skipping 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <cstdlib>
#include <stdlib.h>
#include <string>
#include <cmath>
#include <iomanip>
#include <conio.h>

using namespace std;

void main()
{
	srand(time(0)); //Seeding random number
	int number = rand() % 100 + 1; //The number generator
	int input; //The user's number
	char answer;	//Yes, low, high answer
	int i = 0; //Loop counter

//Introducing the game to user
	cout << "This is a number guessing game, the range will be from 1-100." << endl;
	cout << "I will try to guess your number within 7 tries, I know impressive!" << endl;
	cout << "[PRESS ANY KEY TO CONTINUE]";
	_getch();
	system("cls");
	cout << "Go ahead, type in your digit [1-100] and no decimals please." << endl;
		cin >> input;
		system("cls");
//The do-while loop for the guessing part	
			do
			{
				cout << "Is your number " << number << "?" << endl;
				cout << endl;
				cout << "If the chosen number is too low, type 'low' or if it is too high, type 'high'." << endl;
				cout << "If it is the same, type 'yes'." << endl;
				cin >> answer;
				system("cls");
				switch (toupper(answer))
				{
				case ('LOW'):	//If the chosen number, "number" is too low, the number will be added to half of itself
					cout << "It is too low? Okay, attempt " << i << " used." << endl;
					number = number + (number / 2);
					cout << "Lets try again." << endl;
					cout << "[PRESS ANY KEY TO TRY AGAIN]";
					_getch();
					system("cls");
					break;
				case ('HIGH'):	//If the chosen number, "number" is too high, the half of the number will be subtracted from the number
					cout << "It is too high? Okay, attempt " << i << " used." << endl;
					number = number - (number / 2);
					cout << "Lets try again." << endl;
					cout << "[PRESS ANY KEY TO TRY AGAIN]";
					_getch();
					system("cls");
					break;
				case ('YES'):	//If the user types yes
					cout << "YAY!! I am truely amazing!" << endl;
					cout << "[PRESS ANY KEY TO FINISH]";
					_getch();
					system("cls");
					break;
				}	
				cout << i;
			} while (i++, toupper(answer) != 'YES' || i < 7);
			

//Will go to the if - else statement if yes is inputed or the loop counter "i" reaches 7
			if (i = 7)
			{
				cout << "You have won this time. Lets play again soon!" << endl;
				cout << endl;
			}
			else
			{
				cout << "I have won this time. Lets play again soon!" << endl;
				cout << endl;
			}
}
Last edited on
a char can only hold 1 character so your answer can't be longer than 1 character

'LOW' is longer than 1 character but you declare it as one because of the single quotes
Oh I know, I tried to have toupper(answer) read answer as a string but it wouldn't do it. So I tried LOW HIGH and YES as chars.
you cant make switch/case statements with strings
you have to stick with if/elseif for strings
Duh, I've been out for just a month and forgot that already :p thank you!
You have a number of things going wrong here. First of all, answer is only a character, and you are expecting the user to type a string into it. The stream extractor in line 35 will read in a single character (say 'l' from the word "low") and leave the reset of the input in the input stream. You either need to flush the input stream, or read in a string.

Next, I don't know how your case statements are compiling. The single apostrophe is used to specify a constant character, but you have 3 or 4 characters in statement. If you want to compare the entire word, you need strings. If you want to compare characters, you will only check the first letter of each word.

If you are trying to compare strings you cannot use a switch statement. Switch statements can only be used with integer-type values (so a character is legal, but not a string). So, you can either do the switch with cases for 'L', 'H' and 'Y', or do if/else if/else if/else with "LOW", "HIGH", and "YES".
Oh I fixed it doug4 but thanks! I'll mark it has solved.
Topic archived. No new replies allowed.