I have no errors, but the game isn't working correctly. When I input a letter it states that it is too high even if the letter is "z". It will also only let me input 2 guesses instead of the 5. I am new to C so I really need help.
Code:
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#define MAXGUESSES 5
//function prototypes with a comment for each one describing what the function does.
//Copy and paste from assignment
//this function provides instructions to the user on how to play the game
void GameRules( );
//this function runs one entire game. It for checks either 5 incorrect guesses or a correct guess.
//It returns a 0 if the game is over and the player did not guess the letter, otherwise it returns 1.
int SingleGame(char file_letter);
//this function prompts the player to make a guess and returns that guess
//this function is called from inside the SingleGame( ) function described above
char RetrieveGuess( );
//this function takes two arguments, the guess from the player
//and the solution letter from the file.
//It lets the user know if the guess comes alphabetically before or after the answer
//The function returns 1 if the guess matches the solution and returns a 0 if they do not match
//this function is called from inside the OneGame( ) function described above
int GuessedIt(char answer, char input_letter);
int main()
{
//declare additional variables
int PlayGames = 4,
i = 0;
char letter;
char input_letter;
char answer;
FILE* fptr;
//display instructions
//open file
fptr = fopen("lettersin.txt", "r");
GameRules( );
//get number of games to play
printf("How many games would you like to play today: ");
scanf(" %d", &PlayGames);
input_letter = RetrieveGuess( );
for(i=0;i<PlayGames;i++)
{
//get a letter from file
fscanf(fptr, " %c", &letter);
printf("This is game %d\n", i);
answer = SingleGame(letter);
printf("The letter is: %c\n", letter);
//Play one game
//check for win or lose
}
//close file
fclose(fptr);
return 0;
}
//Function definitions
//copy and paste prototypes and comments from assignment. Then remove the ; from each
//this function provides instructions to the user on how to play the game
void GameRules( )
{
printf("Welcome to the Letter Guessing Game!\n");
printf("You will have a chance to play between 1 to 4 games\n");
printf("You will have 5 chances to guess each letter\n");
printf("Remember that they are all in lowercase\n");
printf("Let's Begin:\n");
return;
}
//this function prompts the player to make a guess and returns that guess
//this function is called from inside the SingleGame( ) function described above
char RetrieveGuess( )
{
char input_letter = 0;
printf("Enter a guess: ");
scanf(" %c", &input_letter);
return input_letter;
}
//this function takes two arguments, the guess from the player
//and the solution letter from the file.
//It lets the user know if the guess comes alphabetically before or after the answer
//The function returns 1 if the guess matches the solution and returns a 0 if they do not match
//this function is called from inside the OneGame( ) function described above
int GuessedIt(char answer, char input_letter)
{
if(input_letter==answer)
{
printf("You guessed it!!!\n");
return 1;
}
else if(input_letter>answer)
{
printf("Your answer %c is too high\n", input_letter);
RetrieveGuess( );
return 0;
}
else if(input_letter<answer)
{
printf("Your answer %c is too low\n", input_letter);
RetrieveGuess( );
return 0;
}
}
int SingleGame(char file_letter)
{
int numGuesses = 0;
while(numGuesses < MAXGUESSES)
{
//RetrieveGuess function call
char RetrieveGuess( );
//GuessedIt function call
int GuessedIt(char answer, char input_letter);
//update counter for number of guesses used
numGuesses = numGuesses +1;
if(numGuesses>MAXGUESSES)
{
printf("You have run out of guesses\n");
}
}
return 0;
}