-
stupid, stupid question
I know this is more than likely very stupid, but my teacher and I cannot seem to get the <cctype> header file to work properly so I can figure out a way to handle the error if someone presses a key on the keyboard since if they do when they are prompted, the program gets all screwed up.
Code:
#include <iostream.h>
#include <iomanip.h>
#include <stdlib.h>
#include <conio.h>
#include <cctype.h>
#include <windows.h>
#include <time.h>
#include <string.h>
void Start(); //sets Start() as a function to be called later in the program for a
//specific piece of code to be called upon
void GetResults(); //set GetResults() as a function to be called later in the program for a
//specific piece of code to be called upon
int j, guess, i, max, Status, perfect; //declares these as interger variables to be used throughout
//the program
char c, ch, string[100]; //declares c to be used as a char variable throughout the program
int main()
{
//This will set the Title bar text
Status = SetConsoleTitle("Jackpot -:::- Can You Guess The Number? ");
HANDLE hOut; //declared hOut as being a type of variable used to be called later
//in the program
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
//This will set the background color and the text color
SetConsoleTextAttribute(hOut,
FOREGROUND_RED |
BACKGROUND_GREEN|
BACKGROUND_INTENSITY|
FOREGROUND_INTENSITY);
//This part of the Program will display information about the program to the user before
//they begin the game
cout << setw(45) << "-_- Jackpot -_-" << setw(35) << " \n";
cout << "The object of the game is to guess the number according to the level you pick.\n";
cout << "You will have 10 guesses to to get this number.\n";
cout << "The game will tell you if you are guessing either too high or too low, and in \n";
cout << "the end will tell you the correct number if you don't guess it.\n\n";
Start(); //calls the Start() function
return 0; //will return control to the computer after the Start() function is executed
}
void Start() //begins the function Start() which was declared earlier in the program
{
//Asks the user to select their level of difficulty
cout << "Select your level of difficulty:\n";
cout << "1 : Novice (0-100)\n";
cout << "2 : Intermediate (0-500)\n";
cout << "3 : Expert (0-1000)\n\n";
cout << "If you would like to quit press another key and then press enter\n";
cin >> c; //Accepts user input and declares it to the c variable
cout << "\n";
switch (c)
{
case '1' : max = 100; // the random number will be between 0 and 100
break;
case '2' : max = 500; // the random number will be between 0 and 500
break;
case '3' : max = 1000;// the random number will be between 0 and 1000
break;
default : exit(0); // exits the program if the user enters something other than 1-3
break;
}
guess = 10; // number of guesses the player has
srand( (unsigned)time( NULL ) );
j = rand() % max; // lets the computer create a random number then turns
// that number into a whole number
GetResults();
}
void GetResults()
{
{
if (guess <= 0) //this will occur if the player runs out of life
{
cout << "How did you not know that the number was " << j << "?\n";
cout << "You can either play again or quit now by entering a letter and enter.\n\n";
Start(); //Repeats the Start() function if this if statement is true
}
cout << "Enter your guess: " << j << endl; //Prompts the user for their guess
cin >> i; // accepts the user's guess
if (guess == 10 && i == j)//displays this if they only used one guess to get the
//correct number
{
cout << "Wow, you are a pure genius, amazing, one try! Or maybe you're just lucky...\n\n";
Start();
}
if (guess == 9 && i == j)//displays this if they only used two guesses to get the
//correct number
{
cout << "Well, it only took two tries, not too bad...\n\n";
Start();
}
if (guess == 8 && i == j)//displays this if they only used three guesses to get the
//correct number
{
cout << "Three tries, that might be average...\n\n";
Start();
}
if (guess == 7 && i == j)//displays this if they used four guesses to get the\
//correct number
{
cout << "You must be a little kid!\n\n";
Start();
}
if (guess <=6 && guess >= 3 && i == j)//displays this if the user used between five
//and eight guesses to get the oorrect number
{
cout << "Ummmm... is this game too difficult for you? That's too many tries.\n\n";
Start();
}
if (guess <= 3 && i == j)//displays this if the user has less than 3 guesses when they
//get the correct number
{
cout << "Would you like me to make a new version for you, one with smaller numbers?\n\n";
Start();
}
if (i == max + 1) // if statement that will execute if the player wants to be funny and
// enter one number higher than their selected range of numbers
{
cout << "Now, I know you think you're funny, but enter a valid number.\n";
GetResults(); //Repeats the GetResults() function if the user enters one number over
} //the max
else if (i >= max + 2 || i < 0) // if the player enters an invalid number
{ // this if statement will be executed
cout << "Error Number 1:Your number is not within the level of difficulty you picked.\n";
GetResults(); //Will pick up the GetResults() function if the user enters an
} //invalid entry
else if (i > j) // this if statement will be executed if the user guesses too high
{
cout << "You guessed too high, try a smaller number.\n";
guess = guess - 1; // subtracts one guess from their previous total if they guess wrong
//Shows the user their remaining amount of guesses
cout << "Number of remaining guesses: " << guess << "\n\n";
GetResults(); //Starts the GetResults() function over after the user makes an
} //incorrect guess
else if (i < j) // this if statement will be executed if the user guesses too low
{
cout << "You guessed too low, try a larger number.\n";
guess = guess - 1; // subtracts one guess from their previous total if they guess wrong
//Shows the user their remaining amount of guesses
cout << "Number of remaining guesses: " << guess << "\n\n";
GetResults(); //Starts the GetResults() function over after the user makes an
} //incorrect guess
}
}
-
just at first glance, you should get rid of the .h suffix to your header files.. with the exception of windows.h
-
well... that didn't work, i'm using Visual C++ 6 and it just keeps going down the list saying it can't recognize <stdlib> and then it if you add the .h to the end of it then it's ok and it will just give you another error for the rest of then... like it will say in doesn't recognize <conio>, then you fix that one and it gives you the next error for the next one, so forth and so on...
-
hmm.. then try <ctype.h> which i think is the older version of <cctype> if i remember correctly
-
just check cin's return value, something like:
Code:
while(!(cin >> value)) {
cout << "Invalid Input!" << endl;
cin.clear();
cin.ignore();
}
also you should avoid recursive menu functions like the plague - it'll eat up your stack before long...
-
I'm not sure I understand your problem correctly, but it looks like you have a lot more headers than you need. Try with just:
Code:
#include <iostream>
#include <cstdlib>
#include <windows.h>
using namespace std;
or if your compiler isn't new enough you would have to use iostream.h and stdlib.h.
I might just not have noticed, but I think those are the only headers you need for the functions and classes you are using.