Thread: Hangman game

  1. #1
    Registered User
    Join Date
    Dec 2019
    Posts
    9

    Hangman game

    Hello, Im here to ask you guys for help, please, I will very grateful. Im doing a hangman game as a school project but it doesnt working at all.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <time.h>
    
    int main(){
    
        int ran=0, i=0, end=0, lenght=0;
        char word [50], word_2[50], name[20];
        int n_words=0, max_attemps, corrects=0, found;
        char letter, c;
    
        printf("Whats your name?\n");
        scanf ("%s", name);
    
        FILE *arch;
    
        arch = fopen("words.txt", "r");
        if (arch==NULL)
        {
            printf ("Erro opening file");
        }
        else {
            while (!feof(arch)){
                    fscanf(arch, "%s", &word);
                    n_words++; // read all the words from the file
    
            }
            n_words--;
            fclose(arch);
    
            srand (time(NULL));
            ran=rand()%(n_words+1); // select random word from the file
            }
    
        arch = fopen ("words.txt", "r");
        if (arch==NULL){
            printf ("Error opening file");
        }else{
            for(n_words=0; n_words<ran; n_words++)
                fscanf(arch, "%s", &word); //  save the selected word
    
    
        }
        fclose(arch);
    
        // Define the max number of attemps
        for (i=0; word[i]!='\0'; i++){
            lenght++;
        }
        printf("%d\n", lenght);
        max_attemps=lenght+2;
    
        //store string with blank characters with the size of the word
        for (i=0; word[i]!='\0'; i++)
            word_2[i] = '\0';
    
        do{
            system("cls");
            // Header of the game
            printf("\n HANGMAN GAME\n\n\n");
    
            // Present letters found
            for (i=0; word[i]!='\0'; i++)
                printf (" %c  ", word_2[i]);
                printf("\n");
    
            // Present positions to the letters
            for (i=0; word[i]!='\0'; i++)
                printf("___ ");
                printf("\n");
    
            // ****PLAYER'S ANSWERS*****
    
            // Read player's answers
            printf("Whats you guess + <enter>: ");
            scanf("%c", &letter);
            scanf("%c", &c);
    
            // Verify if the letter is in the word
            found=0;
            for(i=0; word[i]!='\0'; i++)
            if (word[i] == letter){
                word_2[i] = letter;
                corrects++;
                max_attemps--;
                found = 1;
                printf("Well done, %s",name,"You have now %d attemps", max_attemps);
                printf ("Whats your guess now?");
    
            }
            if(found == 0){
                max_attemps--;
                printf("Oh no, %s",name,"You have now %d attemps", max_attemps);
                printf ("Whats your guess now?");
    
            }
    
            if (max_attemps <= 0 || corrects == lenght) {
                end = 1;
            }
    
        } while (end == 0);
    
        return(0);
    }
    The first problem is that:
    Code:
    printf("Whats your name?\n");
    scanf ("%s", name);
    If I put this the program doesnt work , but if I dont put it works.

    The second is:
    Code:
    printf("Well done, %s",name,"You have now %d attemps", max_attemps);
    printf ("Whats your guess now?");
    It doesnt show up when I compile

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    3,901
    The second one happens because you clear the board before you can see it.

    The first if it is a Compiler error happens because the standard version being used does not allow instructions in the midst of declarations/definitions.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,795
    Quote Originally Posted by Mungo View Post

    The second is:
    Code:
    printf("Well done, %s",name,"You have now %d attemps", max_attemps);
    printf ("Whats your guess now?");
    It doesnt show up when I compile
    That first printf is not valid. It should be
    Code:
    printf("Well done, %s. You have now %d attemps", name, max_attemps);
    Line 94 has the same problem as the above...

    Also lines 25 and 41 are wrong (you should have word and not &word)

    Remove line 78 and change line 77 to
    Code:
    scanf(" %c", &letter);
    and go from there I guess

    Edit: Lines 36-41 are kind of clumsy... you can do what you need to do without having to open the file twice (i.e. you should look at changing something in lines 24-33 to save the word there instead of opening the same file again later just to retrieve the word)
    Last edited by Hodor; 12-06-2019 at 05:43 PM.

  4. #4
    Registered User
    Join Date
    Dec 2019
    Posts
    9
    thanks for the advice. I changed scanf("%s, name) for gets(name) and now its working. I also added a system("pause") to not clear the board before I can see it.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,200
    > thanks for the advice. I changed scanf("%s, name) for gets(name) and now its working.
    No no no no no!!!!
    NEVER EVER USE gets()
    Seriously!
    c - Why is the gets function so dangerous that it should not be used? - Stack Overflow

    It's so awful it's been removed from every language standard.

    Don't use it, forget it ever existed.

    Use fgets() if you want a replacement.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Dec 2019
    Posts
    9
    oh, thanks for the tip. Now Im facing another problem
    Code:
    printf("\nWell done, %s! You have now %d attemps\n\n", name, max_attemps);
             system("pause");
    I put this system("pause") to read the message but now when I get a right letter that have two or more positions in the word it gets from me two or more attempts.
    Do you know what can I do to solve this?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help with hangman game in c#.
    By Kevin Venancio in forum C# Programming
    Replies: 1
    Last Post: 02-20-2018, 05:49 AM
  2. Hangman game
    By GaitBait in forum C Programming
    Replies: 5
    Last Post: 12-07-2013, 12:12 PM
  3. Hangman game
    By GaitBait in forum C Programming
    Replies: 3
    Last Post: 12-06-2013, 01:32 PM
  4. Hangman Game
    By Mcdom34 in forum C# Programming
    Replies: 3
    Last Post: 10-13-2012, 11:21 AM
  5. Help with hangman game.
    By astarialexi in forum C Programming
    Replies: 8
    Last Post: 03-13-2011, 04:04 AM

Tags for this Thread