Help with Hangman program

This is a discussion on Help with Hangman program within the C Programming forums, part of the General Programming Boards category; Need help with a Hangman program. It needs to allow an user to enter a word then let another user ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    1

    Help with Hangman program

    Need help with a Hangman program. It needs to allow an user to enter a word then let another user to guess the word. I think the error is in the Game function, where I am trying to copy the string entered by the first user to a new string, then find the length of the string, then replace the chars with '-', then user 2 is asked to enter a letter and that letter is compared to the original string.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    void Game();
    void DrawGallows(int place);
    
    int main()
    {
     char PlayAgain = 'Y';
    
     printf("-Hangman-\n\n");
    
     while(PlayAgain == 'Y')
     {
      Game();
      printf("\nDo you want to play again, Y or N? ");
      scanf("%c", &PlayAgain);
     }
    
     printf("Thanks for Playing.\n");
    
     return 0;
    }
    
    void Game()
    {
     int Size;
     int Place = 1;
     int Dash;
     char Letter;
     int Correct = 0;
     char WordStr[80];
     char WordCopy[80];
    
     printf("Enter a word: ");
     scanf("%s", WordStr);
    
     strcopy(WordCopy,WordStr);
    
     Size = strlen(WordCopy);
    
     for(Dash = 0; Dash < Size; Dash++)
     {
      WordStr[Dash] = '-';
     }
     WordStr[Dash] = '\0';
     
     while(Place!=6)
     {
      DrawGallows(Place);
      printf("%s", WordStr);
     
      print("Enter a letter :");
      scanf("%c", &Letter);
     
      for(Dash = 0; Dash < Size; Dash++)
      {
     
       if(WordCopy[Dash] == Letter)
       {
        WordCopy[Dash] = Letter;
        Correct = 1;
        printf("\nCorrect!\n");
     
        if(strcmp(WordCopy,WordStr) == 0)
        {
         printf("\nWinner\n");
        return;
        }
       }
      }
      if(Correct == 0)   
      {
       printf("Wrong, Try Again");
       Place++;
      }
      Correct = 0; 
     }   
     DrawGallows(Place);
     printf("The word was: %s", WordStr);
    }
        
    void DrawGallows(int Place)
    {
     if(Place == 6)
     {
      printf("+----+       \n");
       printf("|    |       \n");
       printf("|    O       \n");
       printf("|   /|\\     \n");
       printf("|   / \\     \n");
       printf("| Your Dead  \n");
       printf("=============\n\n");
     }
     else if(Place == 5)
     { 
       printf("+----+       \n");
       printf("|    |       \n");
       printf("|    O       \n");
       printf("|   /|\\     \n");
       printf("|     \\     \n");
       printf("|            \n");
       printf("=============\n\n");
     }
     else if(Place == 4)
     {
       printf("+----+       \n");
       printf("|    |       \n");
       printf("|    O       \n");
      printf("|   /|\\     \n");
       printf("|            \n");
       printf("|            \n");
       printf("=============\n\n");
     }
     else if(Place == 3)
     {
       printf("+----+       \n");
       printf("|    |       \n");
       printf("|    O       \n");
       printf("|   /|       \n");
       printf("|            \n");
       printf("|            \n");
       printf("=============\n\n");
     }
     else if(Place == 2)
     {
       printf("+----+       \n");
       printf("|    |       \n");
       printf("|    O       \n");
       printf("|    |       \n");
       printf("|            \n");
       printf("|            \n");
       printf("=============\n\n");
     }
     else if(Place == 1)
     {
       printf("+----+       \n");
       printf("|    |       \n");
       printf("|            \n");
       printf("|            \n");
       printf("|            \n");
       printf("|            \n");
       printf("=============\n\n");
     }
    }

  2. #2
    Aia
    Aia is offline
    Beautiful to C Aia's Avatar
    Join Date
    Jun 2007
    Posts
    124
    some typing errors noticed:
    strcopy(WordCopy,WordStr); correct syntax is strcpy

    print("Enter a letter :"); correct syntax is printf

    I would suggest that you start learning how to deal with the "Enter" character left behind when you use scanf. That is ruining the flow of the program, as what you want to happens is concern.

    As an example:
    scanf("&#37;c", &PlayAgain); /* you press Y and Enter, scanf reads the character Y and leaves behind "Enter" or "new line " */
    Last edited by Aia; 11-24-2007 at 10:21 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 01:27 PM
  2. Help on hangman program
    By vserenev in forum C++ Programming
    Replies: 1
    Last Post: 01-11-2007, 12:34 PM
  3. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21