These are the errors I\'m getting : \'guess\' : unreferenced local variable - it
ID: 3650154 • Letter: T
Question
These are the errors I'm getting :'guess' : unreferenced local variable - it points to line 3 in main function
'solution' : unreferenced local variable - points to solution in "GetLetters' function def
'CompareLetters' : not all control paths return a value - points to the first and last brackets of the function def 'CompareLetters'
#define _CRT_SECURE_NO_DEPRECATE
#include <stdio.h>
#include <ctype.h>
#define MAXGUESSES 6
//this function provides instructions to the user on how to play the game
void Instructions();
//this function runs one game. It for checks either 6 incorrect guesses or correct guess.
//It returns a 0 if the game is over and the player did not guess the letter, otherwise it returns 1.
int PlayGuess(char solution);
//this function prompts the player to make a guess and returns that guess
//this function is called from inside the PlayGuess() function described above
char GetLetter();
//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
int CompareLetters(char guess, char solution);
int main()
{
int i = 0;
int gamesToPlay = 0;
char guess;
char solution;
FILE *infile;
infile = fopen("inputLet.txt", "r");
Instructions();
//get number of games the user wants to play
printf("How many games do you want to play (1-5): ");
scanf("%d", &gamesToPlay);
for(i=0;i<gamesToPlay;i++) //print current game (value of i)
{
//get letter to guess from file
fscanf(infile, "%c", &solution);
PlayGuess(solution);
printf(" The letter is %c ", solution);
}
fclose(infile);
}
//to displays the instructions
void Instructions()
{
printf("Welcome to Letter Guess ");
printf("You will enter the number of games you want to play (1 - 4 games) ");
printf("You have 6 chances to guess each letter ");
printf("Let's begin: ");
}
int PlayGuess(char solution) //plays the game and compares, calling CompareLetters. Uses
{
int numGuesses = 0;
char guess;
while(numGuesses < MAXGUESSES)
{
numGuesses++;
guess = GetLetter();//user's guess from GetLetter function
if (! CompareLetters(guess, solution))//CompareLetters is called, comparing guess to solution
{
printf("You did not guess the letter ");
}
else
{
printf("You win! :D ");
return 1;
}
}
printf("You lose! :( ");
return 0;
}
//get the guess from the user
char GetLetter()
{
char guess=0;
char solution;
printf("Enter a guess: ", guess);
scanf(" %c", &guess);
return guess;
}
//compare the guess and the solution, return a 1 if they are the same, 0
//if not the same. shown if guess is before or after answer if not the same.
int CompareLetters(char guess, char solution)
{
if (guess == solution) //if the guess is the same as the answer
{
printf("Thats it! ");
return 1; //value of 1 is returned, which means the answer is correct
//the game should stop
}
else
if (guess < solution) //if the guess is after user's guess
{
printf("the letter you are trying to guess comes after %c ", guess);
printf(" Try again ");
GetLetter();//GetLetter is called to try again
return 0;
}
else
if (guess > solution)//if the guess is before user's guess
{
printf("the letter you are trying to guess comes before %c", guess);
printf(" Try again ");
GetLetter();//GetLetter is called to try again
return 0;
}
}
Explanation / Answer
declare the variables as arrays; char guess; instead declare it as char guess[3]; char solution ; instead declare it as char solution[3]; as the reason it is in a loop which repeat s for 3 times. you should pass this value with declaring a new variable every time
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.