Programming C The Hangman Game - Array/Strings Help

Click For Summary

Discussion Overview

The discussion revolves around programming a Hangman game in C, focusing on handling user input, reading words from a text file, and comparing guessed characters to the correct word. Participants are addressing issues related to character comparison, file reading, and managing user guesses.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant expresses a desire to understand the Hangman project better by coding it independently, seeking help with comparing characters from a string read from a file.
  • Another participant clarifies that fscanf reads entire words with %s and suggests using %c for single character input, emphasizing the need to compare characters individually.
  • A participant acknowledges the assumption that user input will not exceed the array size and outlines their approach to reading words from a text file for the game.
  • Concerns are raised about correctly checking if a guessed letter matches any character in the word, with a function provided for comparison.
  • A participant identifies a logic error in their letter-checking function, realizing that the return statement for incorrect guesses needs to be outside the loop.
  • Another participant raises a question about how to store user guesses in an array for later use, indicating a need for managing guessed letters.

Areas of Agreement / Disagreement

Participants generally agree on the need for proper character comparison and file reading techniques, but there are unresolved issues regarding the implementation details and logic errors in the code.

Contextual Notes

Participants discuss potential issues with buffer overflows when using fscanf and the limitations of C compared to C++ for safe string handling. There are also assumptions about user input that may not be explicitly validated.

Who May Find This Useful

Individuals interested in programming games in C, particularly those looking to understand file handling and character comparison in the context of user input.

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
4K
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
3K
  • · Replies 6 ·
Replies
6
Views
3K