1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Programming C The Hangman Game - Array/Strings Help

  1. Nov 5, 2011 #1
    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 (Text):
    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: Nov 5, 2011
  2. jcsd
  3. Nov 6, 2011 #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 (Text):
    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: Nov 6, 2011
  4. Nov 6, 2011 #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 (Text):
    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 (Text):
    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.
     
  5. Nov 10, 2011 #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 (Text):

    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: Nov 10, 2011
  6. Nov 11, 2011 #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 (Text):

    char guessed_letter[SIZE]; //character that is added with each guess
    char user_guesses; //temporary variable that holds user guess
    user_guesses=GetGuess();
     
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Programming C The Hangman Game - Array/Strings Help
  1. C++ strings help (Replies: 2)

  2. C program help needed (Replies: 3)

  3. Help with C++ strings (Replies: 3)

Loading...