When I run the program by itself, it works but it will give my the error message only when I print two or more characters. If I enter a,b,c it sees them as too low.
And when I add this to my full program as a function it gives me this error [Error] 'EOF' undeclared (first use in this function)
[Note] each undeclared identifier is reported only once for each function it appears in.
Code:
#include <stdlib.h>#include <string.h>
#include <time.h>
#include <ctype.h>
void instructions() {
printf("\nHere are the instructions:\n");
printf("1. Pick a number between 1 and 100 and the computer will tell you if it's too high or too low.\n");
printf("2. Pick a number between 1 and 100 and the computer will tell you \n if it's too high or too low and validate your input.\n");
}
void subMenu() {
printf("Additional instructions:In game one you must guess my secret number between 1 and 100. I will tell you when you're correct");
printf("In game two the computer will select a random number from 1 to 100 and you have to guess it");
}
void user_guess() {
int secretnum=5, guess, tries = 0;
printf("Guess My Secret Number Game\n\n");
// Takes user input and compares it to the secret number stored in the secretnum variable
do
{
printf("Guess My Secret Number. It's between 1 and 100 : ");
scanf("%d", &guess);
tries++;
if (guess > secretnum)
{
printf("Too high!\n\n");
}
else if (guess < secretnum)
{
printf("Too low!\n\n");
}
else if (guess == secretnum)
{
printf("That is correct!");
}
}while (guess != secretnum);
return 0;
}
void computer_guess() {
int num,flush, guess, tries = 0;
srand(time(0)); /* seed random number generator */
num = rand() % 100 + 1; /* random number between 1 and 100 */
printf("Guess The Computer's Serect Number\n\n");
// Takes user input and compares it to the random number chosen
do
{
printf("Enter a guess between 1 and 100 : ");
scanf("%d", &guess);
while( (flush=getchar()) != EOF && flush != '\n');
tries++;
if (guess > num)
{
printf("Too high!\n\n");
}
if (guess < num)
{
printf("Too low!\n\n");
}
if ( scanf("%d", &guess) == 1 ) {
} else {
printf("That is not a valid number\n");
}
}while (guess != num);
return 0;
}
main(void)
{
int menu_choice;
printf("\tGUESSING GAME \n");
//Menu list of choices
printf("1. Show Me The Instructions.\n");
printf("2. Guess My Secret Number.\n");
printf("3. Guess The Computer's Secret Number.\n");
printf("0. Quit\n");
do
{
printf("Enter your choice: ");
scanf(" %d", &menu_choice);
switch (menu_choice)
{
case (1): instructions();
case(2): subMenu();
break;
case (3): user_guess();
break;
case (4): computer_guess();
break;
case (5): exit(1); //Exits the program.
break;
//Validates input
default: printf("\n%d is not a valid choice.\n", menu_choice);
printf("Try again.\n");
break;
}
} while ((menu_choice < 1) || (menu_choice > 4));
return 0;
}