this is a basic program for playing a number game where you get 6 guesses to find a number, however im getting an error for 'gotit' not being assigned to a variable that is used in the function main and i can't seem to find the error
Code:
#include <stdio.h>
#define TRUE 1
#define FALSE 0
#define THENUMBER 17
#define MAXGUESSES 6
void greet(void);
int prompt1(void);
void prompt2 (void);
int getresp(void);
int playagain(void);
void goodbye(void);
int main()
{
int play,gotit,thenumber,ans,numguesses;
greet();
play = TRUE;
gotit = FALSE;
while(play){
thenumber= prompt1();
gotit = FALSE;
numguesses = 0;
while(!gotit&& numguesses <MAXGUESSES) {
prompt2();
ans=getresp();
numguesses++;
if(ans==thenumber){
printf("****correct!!!!*****\n\n");
gotit=TRUE;
play=playagain();
}
else {
if(ans<thenumber)
printf("too low!\n");
else if (ans>thenumber)
printf("too high!\n");
gotit=FALSE;
}
}
if (numguesses >=MAXGUESSES)
printf("too many guesses\n");
}
goodbye();
return 0;
}
void greet(void)
{
printf( "Welcome to the game of Guess It!\n" );
printf( "I will choose a number between 1 and 100.\n" );
printf( "You will try to guess that number. If you guess wrong,\n" );
printf( "I will tell you if you guessed too high or too low.\n" );
printf( "You have 6 tries to get the number.\n" );
}
int prompt1(void)
{
printf("\nOK, I am thinking of a number. Try to guess it.\n\n");
return THENUMBER;
}
void prompt2(void)
{
printf("your guess?");
}
int getresp(void)
{
int resp;
scanf("%d",&resp);
return(resp);
}
int playagain(void)
{
int iochar;
while((iochar=getchar())!='\n') /*clears input buffer*/
;
printf("want to play again?");
iochar =getchar();
if(iochar=='y' || iochar=='Y')
return 1;
return 0;
}
void goodbye (void)
{printf("goodbye, it was fun. play again sometime.\n");
}