Hello, I am very new to programming and am having an issue with this letter guessing game. I believe the error is a segmentation fault but haven't been able to find the root problem.
I know that the program executes perfectly the first time the function GuessTheLetter is called but then when my return statement at the end of that function runs my program seems to return to the function GuessTheLetter at which point it will execute again but with an unknown value for letter.
I don't understand why it is returning to the function call instead of the start of the for loop
(the program is formatted better in the attachement)
Code:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <ctype.h>
#define MAXGUESSES 5
//this function provides instructions to the user on how to play the game
void LetterGuessRules();
//this function runs one game.
//input: character from the file, void return type
//all other functions to Play one round of a game
//are called from within the GuessTheLetter function
void GuessTheLetter(char solution);
//this function prompts the player to make a guess and returns that guess
//this function is called from inside the GuessTheLetter( ) function described above
char GetTheGuess();
//this function takes two arguments, the guess from the player
//and the solution letter from the file.
//The function returns 1 if the guess matches the solution and returns a 0 if they do not match
//This function also lets the user know if the guess comes alphabetically before or after the answer
int CompareLetters(char let1, char let2);
int main()
{
//all declarations for function main
FILE * LtrLst;
int numGames, i = 0;
char letter;//letter from file
//display game rules
LetterGuessRules();
//Ask and get number of games to play
printf("So, how many times will you be wagering your soul? ");
scanf("%d", &numGames);
//connect to the file HINT: use fopen
LtrLst = fopen("letterList.txt", "r");
//this for loop will allow the player to play more than one game
//without recompiling
for (i = 0; i < numGames; i++)
{
//get a solution letter from file - use fscanf
fscanf(LtrLst, "%c", &letter);
//change the solution to lowercase
letter = tolower(letter);
//print the solution back onto the screen to test
printf("%c", letter);
//call the GuessTheLetter function and pass it the solution
GuessTheLetter(letter);
}
//close file pointer
fclose(LtrLst);
return 0;
}
//this function provides instructions to the user on how to play the game
void LetterGuessRules()
{
//display game rules
printf("So you wanna play my little game? The rules are simple, what (letter) do I have in my pocket? You have 5 guesses to figure out what letter I have. Fail and you will be subject to eternity locked in cyber space!\n");
return 0;
}
//this function runs one game.
//input: character from the file, void return type
//all other functions to Play one round of a game
//are called from within the GuessTheLetter function
//this function lets the user know if they have won or lost the game
void GuessTheLetter(char solution)
{
int win = 0;
int numGuesses = 0;
char userguess;
int trialrem;
//declare additional variables
while (numGuesses < MAXGUESSES && win == 0)
{
trialrem = 5 - numGuesses;
printf("You have %d guess left ", trialrem);
//get a guess from the user by calling the GetTheGuess function
userguess = GetTheGuess();
//change the guess to lowercase
userguess = tolower(userguess);
//win = call the function to compare the guess with the solution
win = CompareLetters(solution, userguess);
numGuesses++;//count the number of guesses so far
}
//use conditions to let the user know if they won or lost the round of the game
if (win == 1)
printf("You chose... Wisely ");
else
printf("You chose... poorly ");
return 0;
}
//this function prompts the player to make a guess and returns that guess
//this function is called from inside the GuessTheLetter( ) function described above
char GetTheGuess()
{
char input;
printf("What is your guess?\n");
scanf(" %c", &input);
return input;
}
//this function takes two arguments, the guess from the player
//and the solution letter from the file.
//The function returns 1 if the guess matches the solution and returns a 0 if they do not match
//This function also lets the user know if the guess comes alphabetically before or after the answer
int CompareLetters(char let1, char let2)
{
if (let1 == let2)
{
return 1;
}
else if (let1 < let2)
{
printf("\nMy letter comes before your guess\n");
return 0;
}
else
{
printf("\nMy letter comes after your guess\n");
return 0;
}
}