For starters, make sure your code is neatly formatted and indented. For example:
Code:
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <ctype.h>
#define MAXGUESSES 5
//Taken from paper printed from blackboard assignment
//This function provides instructions to user.
void Instructions( );
//This function runs one game.
//It returns a 0 if the game is over.
int PlayGuess(char solution);
//This function prompts the player to make a guess.
//This function is called from inside the PlayGuess( ) function.
char GetLetter( );
//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
int CompareLetters(char guess, char solution);
int main()
{
int i=0;
int numgames=0;
char solution;
char guess;
int compareletter(char guess, char solution);
FILE *inp;
inp = fopen("inputLet.txt","r");
fscanf(inp,"%c",&solution);
Instructions();
//# of game player wants to do
printf("Please enter the number of games you want to play\n");
scanf("%d",& numgames);
//display the value of game
for(i=1;i<=numgames;i++)
{
//get letter to guess from file
fscanf(inp, "%c", &solution);
PlayGuess(solution);
printf("\nThe letter is %c\n" , solution);
}
fclose(inp);
}
void Instructions( )
{
printf("Letter guessing game\n");
printf("Enter how many time you want to try(1 – 4 games)\n");
printf("You have 5 chances to guess each letter\n");
printf("Start now\n");
}
int PlayGuess(char solution) //player defined guesses.
{
int numGuesses=0;
int winOrLose =0;
while(numGuesses < MAXGUESSES)
{
GetLetter();
numGuesses = numGuesses +1;
if(numGuesses>MAXGUESSES)
{
printf("You have run out of guesses\n");
}
}
return 0;
}
//gets guess form player(getLetter function).
char GetLetter()
{
char guess=0;
char solution;
printf("Enter a guess:", guess);
scanf(" %c",&guess);
CompareLetters(guess,solution);
return guess;
}
//compare the guess and the solution
//returns 1 if same
// message based on before or after alphabetically
//returns 0 if guess & answer arn't same
int CompareLetters(char guess, char solution)
{
if (guess==solution) //if answer is right
{
printf("Great Job!\n");
return 1;
}
else if (guess<solution)
{
printf("letter you are looking for comes after %c\n", guess);
printf("\ntry harder\n");
GetLetter();
return 0;
}
else if (guess>solution)
{
printf("letter you are lookingfor comes before %c", guess);
printf("\ntry harder\n");
GetLetter();
return 0;
}
}
Second, make sure your compiler warnings are turned up. Here are some I received when I compiled:
Code:
/*
main.c||In function 'main':|
main.c|30|warning: unused variable 'guess'|
main.c||In function 'PlayGuess':|
main.c|64|warning: unused variable 'winOrLose'|
main.c|61|warning: unused parameter 'solution'|
main.c||In function 'CompareLetters':|
main.c|118|warning: control reaches end of non-void function|
main.c||In function 'GetLetter':|
main.c|87|warning: 'solution' is used uninitialized in this function|
||=== Build finished: 0 errors, 5 warnings ===|
*/
Those last two warnings are pretty significant.
Lastly, you need to be more descriptive with your problem. "The code is off" is not a sufficient description. Tell us what is not working; what input you give it, what output you get, what output you expect. Since your program relies on reading a file, you should post the contents of that file, as well.
Without a decent problem description, it will not be easy to help you. I did notice one thing while reading through it quickly. The variable "solution" in "GetLetter()" is not the same variable as "solution" in "PlayGuess()". The correct way to do this would be to pass the variable "solution" to "GetLetter()". Even better would be to just have "GetLetter()" read a character and return it back to "PlayGuess()", and do the comparison there. Keeping your logic organized will help with development and troubleshooting.
Also, you should check that the file has opened successfully before trying to access it