Hi,
I need help with me Hangman code below please. I have only been programming for 5 weeks so my lecturer isn't concerned with memory efficiency etc, only that the program works. The part that I am stuggling with is when the user enters the letter and then for it to be checked against the array. I can get it to check the array and print out if it finds the letter, however if it doesnt find the letter, i am having problems......if you could please look at my code and let me know....thanks...
Code:
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
char words[10][10]; /*array to store imported file strings*/
int buffer; /*temporarily stores random word from words array*/
char gameword[10]; /*array to store playable gameword*/
int i; /*counter*/
int option; /*Menu option*/
int lives; /*Number of game lives*/
char letter; /*stores letter guessed*/
char guessedword[10]; /*all letters will be set to "*" and changed as the user guesses correctly*/
int counter;
int number;
int x;
FILE *file;
file = fopen("test.txt", "r");
i = 0 ;
while(!feof(file))
{
/* loop through and store the numbers into the array */
fscanf(file, "%s", &words[i]);
i++;
}
fclose(file);
srand(time(NULL)); /*These 3 lines of code generate a random*/
buffer=words[(1 + rand() % 4)]; /*word from the imported file into the buffer*/
strcpy(gameword, buffer); /*and then copies it into a playable array*/
number = strlen(gameword);
for ( counter = 0; counter < number; counter++)
{
guessedword[counter] = *"*"; /*sets all guessedword characters to "*" */
}
/*Menu text*/
printf("Hello and Welcome to Hangman in C\n\n");
printf("Please choose one of the following options: \n\n");
printf("1. Beginner Difficulty - 9 Lives\n");
printf("2. Intermediate Difficulty - 6 Lives\n");
printf("3. Hard Difficulty - 3 Lives\n");
printf("4. Exit Game\n\n");
printf("Make your selection: ");
scanf("%d", &option);
if (option == 1) /*Determines number of games lives depending upon*/
/*user input*/
{
lives = 9;
}
else if (option == 2)
{
lives = 6;
}
else if (option == 3)
{
lives = 3;
}
else if (option == 4)
{
return 0;
}
system ("cls"); /*Clears screen*/
if (option == 1) /*Displays message depending upon user input*/
{
printf("\nSo you've gone for the easy option - What's the matter, feeling chicken?\n\n ");
}
else if (option == 2)
{
printf("So you're a middle of the road type of guy then!\n\n ");
}
else if (option == 3)
{
printf("So you've gone for the Hard difficulty - Good luck, you are going to hang\n\n");
}
printf("%s\n\n\n", gameword);
printf("%s", guessedword);
printf("Please choose a letter ");
scanf("%s", &letter);
for (x=0; x<number; x++)
{
if (letter == gameword[x])
{
guessedword[x] = letter;
printf("You have made a correct guess\n\n%s", guessedword);
}