Programming C The Hangman Game - Array/Strings Help

Click For Summary
SUMMARY

The discussion focuses on developing a Hangman game in C, specifically addressing issues with reading words from a text file and comparing user guesses to the characters of the word. The user initially struggles with using the fscanf function to read individual characters, as it reads entire words instead. The solution involves using %c for character input and implementing a comparison function to check user guesses against the correct letters. Additionally, the user identifies a logic error in their letter-checking function, which they resolve by adjusting the return statement.

PREREQUISITES
  • Understanding of C programming syntax and structure
  • Familiarity with file handling in C, specifically using fopen and fscanf
  • Knowledge of character arrays and string manipulation in C
  • Basic understanding of control structures, such as loops and conditionals
NEXT STEPS
  • Implement safe file reading techniques using scanf_s or fscanf_s to prevent buffer overflows
  • Learn about dynamic memory allocation in C to handle variable-length words
  • Explore advanced string manipulation functions in C, such as strncmp and strcspn
  • Research best practices for implementing game logic in C, including state management and user input validation
USEFUL FOR

Beginner to intermediate C programmers, game developers looking to implement text-based games, and anyone interested in improving their understanding of file I/O and character manipulation in C.

denZer
Messages
17
Reaction score
0
I'm well aware of all the Hangman codes that are out there given, but I feel that I will get a better understanding of this project if I did it myself with some help.

My main problem is knowing how to compare one character of array from a string text file. The text file consist of a whole word, but the user must guess each character right. From my main function, this is what I have:

Code:
int main ()
{
	//variable that stands for the amount of guess from user
	int TotalGuess;
	int i;
	int MaxGuesses;
	MaxGuesses=6;
	//guesses of user starts from 0
	TotalGuess=0;

	//variable that stands for the guess of the user
	char UserGuess;
	char answer;
	FILE *inp;
	char filestrings[SIZE];
	//number of game always start at 0 and will be incremented by 1 when user plays more
	Instructions();
    inp=fopen("words.txt", "r");
	for(i=0;i<MaxGuesses;i++)
	{
		//getting a letter from file
		fscanf(inp, " %s", filestrings);
		char UserGuess();
		
	}



}

For the fscanf, I don't know how to have it compare one character array at a time because it'll compare the whole words rather than one by one.
 
Last edited:
Physics news on Phys.org
The function fscanf is for performing formatted input from a file, not for making comparisons. If you have %s in the format string, then fscanf will read the entire word with one call. The word from the file only needs to be read once, not every time the user makes a guess. To read a single character you may use %c.

You may compare chacters using the == operator. Each character of the word from the file would have to be compared to the corresponding character of the users guess (assuming the user has entered a guess with the correct number of characters).



Code:
char UserGuess();
This is a function declaration.
Also, you already had UserGuess declared as a character in the function's scope.


Another thing is that any format string with %s used with scanf or fscanf is unsafe. These functions don't prevent buffers overflows, which would happen when the word is longer than the array size. Microsoft provides the nonstandard scanf_s and fscanf_s which allow protection from buffer overflow.

Unfortunately, there is no easy standard solution to this problem with C. With C++ it is simple to do formatted input of strings safely.
 
Last edited:
You are right, I can only assume that the user doesn't enter the chars longer than the array itself. I don't have to worry about that problem for this project so I'll be fine.

The thing is, I want it to read the .txt file so that it knows what the words are. So far, my words.txt file goes something like this:

store
apple
tennis
games
etc

-all on new lines, but like you said, I want the user to guess out the individual character of 'store' and this is what I came up with:

Code:
int main ()
{
	//variable that stands for the amount of guess from user
	int TotalGuess;
	int i;
	int MaxGuesses;
	char filestrings[SIZE];
	FILE *inp;

	char StoreUserLetter[SIZE];
	MaxGuesses=6;
	//guesses of user starts from 0
	TotalGuess=0;
	i=0;


	//number of game always start at 0 and will be incremented by 1 when user plays more
	Instructions();
	for(i=0; i<SIZE; i++)
    {
		StoreUserLetter[i]=GetGuess();
    }
	printf("You guessed: ");
	for(i=0; i<SIZE; i++)
	{

		printf("%c", StoreUserLetter[i]);
	}
	printf("\n");
	inp=fopen("words.txt", "r");
	fscanf(inp, " %c", filestrings);
	printf(" %s\n", filestrings);
}

The two forloops that you see are supposed to be stored in what the user wants (with a certain amount of tries). I need to first make it so that when the user enters one letter in, it'll check to see if it matches with any of the letter that spells out 's-t-o-r-e'.

and I do have that user function that checks to see if it's right or wrong right here:
Code:
char CompareGuess(char userguess, char answer)
{

	int win=0;
	if(userguess==answer)
	{
		printf("You guessed %c, which is right!", userguess);
		win=1;
	}
	else
	{
		printf("You guessed %c, which is wrong.", userguess);
	}
	return win;

}

I guess it all goes down to if I did the coding right within the file reading so that a single string is read but it checks to see if each char is right or wrong.
 
I've came across one problem; in the words.txt file, the first string contained is 'store'. However, if the user guesses the character 't', it still shows up as being incorrect while only the letter 's' is the correct one,

Here is the function that checks to see if the letter is correct:
Code:
int CheckLetter(char letter_guessed, char word_to_be_guessed[])
{
	int i;
	for(i=0;i<strlen(word_to_be_guessed);i++)
	{
		if(letter_guessed==word_to_be_guessed[i])
		{
			return i;
		}
		else
			return -1;
	}
}

EDIT: nvm I found the problem. The return -1. needs to be outside!
 
Last edited:
So I've ran into an issue, I will have to eventually use an array called "guessed_letters" for now, I am using char user_guesses to store the temporary chars that the user enters. My question is, how can I add their guesses to the array of guessed_letter later on?

Code:
char guessed_letter[SIZE]; //character that is added with each guess
char user_guesses; //temporary variable that holds user guess
user_guesses=GetGuess();
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
Replies
8
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K