Validating an input to be a number for guessing game
Finally, an update to the number guessing game. I've come back to it after all this time, and had to start from scratch (because of upgrading to Vista, which I'm not sure if I regret or not). However, I think I got a better result anyway, as this time, I not only managed the loop for replay, I put in a minor menu, added difficulty settings, and put in a number validation sequence to make sure the number input is in the given range. Here's what I've got:
Code:
#include <cstdlib>
#include <ctime>
#include <iostream>
using namespace std;
//returns value between 0 and RAND_MAX
//rand();
//srand (time(0));
int x,w,random,guess,tries,option;
char play;
int game (){
do {
cout << "\nEnter a difficulty level: \n1. Easy\n2. Medium\n3. Hard\n4. Very Hard\n5. Insane!\n\n";//States possible difficulties
cin >> w;
if (w==1) x = 50;
if (w==2) x = 100;
if (w==3) x = 500;
if (w==4) x = 1000;
if (w==5) x = 10000;//Determines upper limit for random generator
srand(time(0));//Random seed
random = rand() % x + 1;//Random number generation
cout << "\nNumber chosen between 1 and " << x << ".\n";
cout << "Enter your guess.\n";
cin >> guess;
tries = 1;//Sets tries to one after first guess before going into the game loop
while (guess != random){
if ((guess > x) && (guess < 1)) {
cout << "\nIllegal guess. Your guess must be between 1 and " << x << ".\n";//Keeps player from inputting a number that is not available
--tries;//Sets counter back to keep track of legal guesses only
}
else if (guess > random)
cout << "\nYou guessed too high.\n";
else
cout << "\nYou guessed too low.\n";//The actual process of guessing the number
cout << "Enter your guess.\n";
cin >> guess;
++tries;
}
cout << "\nYou Guessed It!\n";
cout << "You took " << tries << " guesses.\n";//Displays number of legal guesses required to win current round
cout << "Would you like to play again (Y/N)?\n";//Asks if player wants to play again, feeds into do...while loop parameter
cin >> play;
} while (play == 'y');
return 0;
}
int menu(){
cout << "Main Menu: \n1. New Game \n2. Quit\n";
cin >> option;
if (option == 1)
game();
else
return 0;
}
int main(){
cout << "Welcome to Guess It, the number guessing game.\n";
menu();
return 0;
}
Now, right now I have two problems.
1) I can't find any reasonable resource for validating the input as a number, instead of a letter or punctuation. I've tried something like this:
Code:
cin.ignore(numeric_limits<int>::max(), '\n');
while (guess != random){
if (!cin || cin.gcount() != 1){
cout << "\nIllegal guess. Your guess must be a number.\n";
As the first if... statement in the game loop, but although it works and it displays the "Illegal guess" line and the "enter your guess" line later, it does it on an infinite loop, and I can't get it to do it just once and accept another input.
2) I don't know how to make it go back to the menu after the player says "no" to the play again question.
Any and all help will be greatly appreciated.
Edit: OK, I got the menu to work. All I needed to do was add a parameter (I chose on) to tell whether or not the program was supposed to still be running. I then put a do...while loop in main() to keep going back to the menu as long as on == 1. I then put an extra line in menu() to say if the player doesn't want to start a new game, but instead wants to quit, on is set to 0. So, the changes look like so:
Code:
int menu(){
cout << "Main Menu: \n1. New Game \n2. Quit\n";
cin >> option;
if (option == 1)
game();
else
on = 0;
return 0;
}
int main(){
cout << "Welcome to Guess It, the number guessing game.\n";
do {
menu();
} while (on == 1);
return 0;
}
Still need help on the letter elimination problem...