Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
void matchWord(char *word, char *correctStr, int *correct, int *incorrect, char inputStr[], int *numberInput);
char *wordTable(int n);
void displayEmpty(char *string, int length);
int main(void)
{
char *word; //store the word from table
char *correctString= malloc(255); //store the correct string
char UserInput[24]; //store user inputs (avoid duplicate entry)
char again; //for user play again
int length; //length of the word
int correct; //number of correct guess
int incorrect; //number of incorrect guess
int numberInput; //number of letters that user guests
int n; //random number for the word table
int i = 0; //declaration of i for for loop
printf("\nAre you ready to play? (y for YES, or n for NO) ");
scanf("%c", &again);
while(again != 'n')
{
correct=0;
incorrect=0;
numberInput=0;
for(i = 0; i < 24; i++)
{
UserInput[i] = '\0';
} //end for
srand((unsigned)time(NULL)); //seed random number using current time
n = rand()% 20;
char table[][20]={"literature", "scholar", "enormous", "influence", "publication", "pioneer", "reshape", "catalyst", "leader", "member", "final", "phonograph", "executed", "oldest", "people", "requiring", "screwdriver", "buckshot", "different", "striking"};
word = table[n];
printf("%c", *word);
/* word = wordTable(n); //random number */
length = strlen(word);
displayEmpty(correctString, length);
while(incorrect < 7 && correct < length)
{
matchWord(word, correctString, &correct, &incorrect, UserInput, &numberInput);
printf("Letter used: \n%c", *UserInput);
} //end while
if(incorrect == 7)
{
printf("\nSorry! You missed.");
printf("The word was: %c", *word);
} //end if
else
{
printf("\nCongratulations! You are correct!");
printf("The word was: %c", *word);
} //end else
printf("\nTry again? (y for YES; n for NO) ");
scanf("%c",&again);
}//end while
return 0;
} //end main
/* wordTable: returns a word from the Word Table */
/*
char *wordTable(int n)
{
char table[][20]={"literature", "scholar", "enormous", "influence", "publication", "pioneer", "reshape", "catalyst", "leader", "member", "final", "phonograph", "executed",
"oldest", "people", "requiring", "screwdriver", "buckshot", "different", "striking"};
return table[n];
} // end wordTable */
/* displayEmpty */
void displayEmpty(char *string, int length)
{
int i;
char *ch = "-", *eos = "";
strcpy(string,eos);
printf("\n");
for(i = 0; i < length; i++)
{
strcat(string, ch);
printf("%c", *ch);
}
printf("\n");
} //end displayEmpty
/* MatchWord */
void matchWord(char *word, char *correctStr, int *correct, int *incorrect, char inputStr[], int *numberInput)
{
char *temp, *temp2, *temp3, input;
int correctFlag = *correct;
int n = 0;
printf("\nWhat letter do you guess? ");
input = getchar();
while(getchar() != '\n');
/* scanf("%c", &input);*/
temp3 = inputStr;
while(*temp3 != '\0')
{
if(input == *temp3)
{
printf("This letter is already chosen.");
printf("%c",*correctStr);
return;
} //end if
temp3++;
} //end while
inputStr[(*numberInput)++] = input;
temp = word;
temp2=correctStr;
while(*temp != '\0')
{
if(input == *temp)
{
*temp2 = input;
(*correct)++;
n++;
temp++;
temp2++;
} //end if
else
{
temp++;
temp2++;
} //end else
}//end while
printf("\n");
printf("%c", *correctStr);
if(*correct == correctFlag)
{
(*incorrect)++;
}
}// end matchWord
Notice that some of the other code has been commented out, such as wordTable. I decided to just stick it in the main function where I had it working in another program. Problem is, that didn't fix the problem of it outputting the word right anyways. I also added a cheat line to tell you what the word is, which it displays as always being a single character.., and even if you guess the character, the program tells you you are incorrect.