My program compiles, however, there are 3 problems with it:
1: A bunch of garbage is print after the asterisks. Example: ****wurywerg - I believe there's something wrong in my fillArray() function..which is right after main ( )
2: My loop never ends! It displays "You have -1 guesses left. Please enter a letter: " and so forth. Is my logic for the for loop okay in play()?
3: It keeps telling the user to guess even AFTER they've figured out all the letters! I believe this has more to do with my for loop logic in play()
Play() is the last function!
Sorry this is so long!
Code:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAXWORD 20 //max word length
#define INCORRECT_GUESSES 9 //max # of guesses allowed
/* Prototypes */
void fill_array( char *theArray, int howMany, char theLetter ); // Fills theArray with howMany copies of theLetter
int get_letter( char *theWord, char *soFar ); // Get char from player, checks the letter, shows progress so far
int letter_in_word( char *theWord, char *soFar, char theLetter ); // Check if letter is in word, updates progress so far
void lower_string( char *someWord ); // Convert the word to lowercase
void play( char *theWord ); // Play one game
/* Function definitions */
int main( )
{
char theWord[ MAXWORD ]; //theWord is an array of characters with the length of MAXWORD
FILE *ifp = fopen( "guesswords.txt", "r" ); //open myFile to read
if (ifp == NULL ) //if file doesn't exist
{
printf( "File does not exist\n" );
return -1; //return code
}
fscanf(ifp, "%s", theWord); //scans word from file
lower_string( theWord ); //invokes the function lower_string
play( theWord ); //passing theWord into play function
fclose(ifp); //closes file
return 0; //all done
}
/* *********************************************************************** */
void fill_array( char *theArray, int howMany, char theLetter ) // Fills theArray with howMany copies of theLetter
{
int i;
for( i = 0; i < howMany; ++i)
{
theArray[i] = theLetter;
}
}
/* *********************************************************************** */
// Get char from player, checks the letter, shows progress so far
int get_letter( char *theWord, char *soFar )
{
char theLetter;
int n;
printf("Please enter a letter: \n");
scanf(" %c", &theLetter);
theLetter = tolower(theLetter);
n = letter_in_word(theWord, soFar, theLetter);
return n;
}
/* *********************************************************************** */
int letter_in_word( char *theWord, char *soFar, char theLetter ) // Check if letter is in word, updates progress so far
{
int i;
int numberOfMatches = 0;
for( i = 0; i < strlen(theWord); ++i )
{
if (theWord[i] == theLetter)
{
soFar[i] = theLetter;
++numberOfMatches;
}
}
if (numberOfMatches == 0)
{
printf("Oops! The letter you guessed is not in the word!\n");
return 0; //not found
}
else
{
printf("Yay! The letter you guessed is in the word!\n");
printf("%s\n",soFar);
return 1; //1 or more was found
}
}
/* *********************************************************************** */
void lower_string ( char *someWord ) // Convert the word to lowercase
{
int i;
for (i = 0; i < strlen(someWord); ++i)
{
someWord[i] = tolower(someWord[i]);
}
}
/* *********************************************************************** */
void play( char *theWord ) // Play one game
{
//char theWord[MAXWORD];
int maxGuesses = INCORRECT_GUESSES - 1;
char soFar[MAXWORD];
int i;
fill_array( soFar, strlen(theWord),'*' );
printf("%s\n",soFar);
for (i = 0; i < INCORRECT_GUESSES; --maxGuesses)
{
int result = get_letter(theWord, soFar);
if ( result == 0 )
{
printf("You have %d more letters to guess! Keep guessing! \n", maxGuesses);
}
else if ( soFar[MAXWORD] == *theWord)
{
printf("You have guessed all letters! Good job! \n");
}
}
printf("You're out of guesses! The word was %s \n", theWord);
}