There's something wrong with my code. It builds no problem, and it starts up okay. But when it asks to enter a letter it gives me a Run-time check failure #3-T error. If I choose to ignore it, I could keep entering guesses, but the error keeps coming back whenever I do.
Code:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <ctype.h>
#define MAXGUESSES 5
void LetterGuessRules();//This function provides instructions to user
int PlayOneGame(char solution);//This function runs one game, it returns a 0 if the game is over
char GetGuess(); //Gets guess from player
int CompareGuessAndSoultion(char guess, char solution);//Takes two arguments, the guess from the player and the solution from the file. Returns a 1 if the guess is correct or a 0 is it's not correct
int GameCount();
int main()
{
int i = 0;
int numgames = 0;
char solution;
char guess;
int compareletter(char guess, char solution);
FILE *inPtr;
inPtr = fopen("letters.txt", "r");
fscanf(inPtr, "%c", &solution);
solution = toupper(solution);
LetterGuessRules();
//# of games player wants to play
int GameCount();
printf("Please enter the number of games you want to play\n");
scanf("%d", &numgames);
for (i = 1; i <= numgames; i++)
//display the value of game
{
//get letter to guess from file
fscanf(inPtr, "%c", &solution);
solution = toupper(solution);
PlayOneGame(solution);
printf("\nThe letter is %c\n", solution);
}
fclose(inPtr);
}
void LetterGuessRules()
{
printf("Welcome to the Letter guessing game\n");
printf("\nEnter how many games you want to play(1-8 games)\n");
printf("You have 5 chances to guess each letter\n");
printf("Let's begin!\n");
}
int PlayOneGame(char solution)
{
int numGuesses = 0;
int winOrLose = 0;
while (numGuesses < MAXGUESSES)
{
GetGuess();
numGuesses = numGuesses + 1;
if (numGuesses>MAXGUESSES)
{
printf("You have run out of guesses\n");
}
}
return 0;
}
char GetGuess()
{
char guess = 0;
char solution;
printf("Enter a guess:", guess);
scanf(" %c", &guess);
CompareGuessAndSoultion(guess, solution);
return guess;
}
//Compares the guess and the solution
//returns 1 if the guess is correct
//returns 0 if guess & answer aren't the same
int CompareGuessAndSoultion(char guess, char solution)
{
if (guess == solution) //If the guess was correct
{
printf("Great Job!\n");
return 1;
}
else
if (guess<solution)
{
printf("The letter you are looking for comes after %c\n", guess);
printf("\nTry harder\n");
GetGuess();
return 0;
}
else
if (guess>solution)
{
printf("The letter you are looking for comes before %c", guess);
printf("\nTry harder\n");
GetGuess();
return 0;
}
}
int GameCount()
{
int i = 0;
int numgames;
printf("Please enter the number of games you want to play\n");
scanf("%d", &numgames);
for (i = 1; i <= numgames; i++);
}