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.
#include <iostream>
#include <cstdlib>
#include <stdlib.h>
#include <string>
#include <cmath>
#include <iomanip>
#include <conio.h>
usingnamespace 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;
}
}
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".