Programming C The Hangman Game - Array/Strings Help

In summary, the conversation discusses the problem of comparing individual characters from a string text file in a Hangman game project. The code provided shows the use of fscanf for formatted input and the risks of using %s, as well as the function declaration for UserGuess. The conversation also mentions the issue of a buffer overflow and the use of scanf_s and fscanf_s to prevent it. The conversation continues with the discussion of using an array to store the user's guesses and how to add those guesses to the array.
  • #1
denZer
17
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
  • #2
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:
  • #3
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.
 
  • #4
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:
  • #5
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();
 

1. How can I create an array to store the words for the Hangman game?

To create an array in C, you can use the syntax: dataType arrayName[arraySize]; In this case, the data type would be a string, and the array size would be the number of words you want to store. For example: char wordList[10]; will create an array with 10 elements to store words.

2. How can I randomly select a word from the array for the Hangman game?

To randomly select a word from the array, you can use the rand() function. First, you will need to seed the random number generator using srand(), passing in a seed value such as time(NULL). Then, you can use rand() % arraySize to generate a random index within the array. This index can then be used to access a word from the array.

3. How can I check if a guessed letter is in the selected word?

One way to check if a guessed letter is in the selected word is by using a loop to iterate through the letters in the word. You can use the strcmp() function to compare the guessed letter to each letter in the word. If there is a match, then the letter is in the word. If there is no match, the letter is not in the word.

4. What happens if the player runs out of guesses in the Hangman game?

If the player runs out of guesses in the Hangman game, they lose the game. You can keep track of the number of incorrect guesses and set a maximum number of guesses before declaring a loss. You can also display a message to the player when they run out of guesses to inform them of the loss.

5. How can I display the Hangman game using ASCII art?

To display the Hangman game using ASCII art, you can use printf() statements to print out different parts of the hangman, such as the gallows, the head, body, arms, and legs. You can also use conditionals to print out different parts depending on the number of incorrect guesses. There are also online resources available for ASCII art of the Hangman game that you can use in your program.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
913
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
11
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
Back
Top