Like Tree2Likes
  • 2 Post By oogabooga

Hangman Project Problem

This is a discussion on Hangman Project Problem within the C Programming forums, part of the General Programming Boards category; hey there...can anyone help to finish this: Code: #include<stdio.h> #include<conio.h> #include<stdlib.h> #include<string.h> int A(); int B(); main () { char ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    29

    Hangman Project Problem

    hey there...can anyone help to finish this:

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    #include<string.h>
    
    
    
    
    int A();
    int B();
    main ()
    {
         char letters, errors, tries;
         int i, j;
         
         for(i=0;i<10;i++)
         {
         printf("\n");
             for(j=0;j<4;j++)
             {
                 printf("\t");
             }
         }
         
         
         printf("*Welcome to HangMan*");
         getch();
         A();
         
         getch();
         return 0;
    }
    
    
    
    
    A()
    {
         int i, j;
         
         system("cls");
         for(i=0;i<10;i++)
         {
         printf("\n");
             for(j=0;j<4;j++)
             {
                 printf("\t");
             }
         }
         
         
         printf("*Welcome to HangMan*\n");
         
         for(j=0;j<4;j++)
             {
                 printf("\t");
             }
    
    
         printf("*Press Enter to play*");
         
         getch();
         B();
    }
    
    
    
    
    B()
    {
         char letter_guess; 
         char letter[100];
         char c, undscr;
         int x=0, i, j;
         int len;
         int loop;
         int total_tries=7;
         int tries;
         int count;
         int disk=0;
         
         system ("cls");
         printf("enter the word to be guess: \n");
         
         do{
               c = getch();
               switch(c)
               {
                   case '\r':
                        break;
                   case '\b':
                        printf("\b \b");
                        if(x>0) x--;
                        break;
                   default:
                           letter[x++]= c;
                           printf("%c", '*');
                           break;
               }
          
           }while(c != '\r');
           letter[x]= '\0';
         
         
         
         system("cls");
    
    
         while(total_tries != 0)
         {
         printf("\n\ntries left: %d", total_tries);
             if(total_tries==7)
             {
                 printf("\n\t\t_______\n\t\t|     |\n\t\t|     \n\t\t|    \n\t\t|    \n\t\t|      \n\t\t|      \n\t\t|      \n\t       _|_\n\t      /___|");
             }
             else if(total_tries==6)
             {
                 printf("\n\t\t_______\n\t\t|     |\n\t\t|     O \n\t\t|      \n\t\t|      \n\t\t|      \n\t\t|      \n\t\t|      \n\t       _|_\n\t      /___|");
             }
             else if(total_tries==5)
             {
                 printf("\n\t\t_______\n\t\t|     |\n\t\t|     O \n\t\t|     T\n\t\t|      \n\t\t|      \n\t\t|      \n\t\t|      \n\t       _|_\n\t      /___|");
             }
             else if(total_tries==4)
             {
                 printf("\n\t\t_______\n\t\t|     |\n\t\t|     O \n\t\t|    |T\n\t\t|      \n\t\t|      \n\t\t|      \n\t\t|      \n\t       _|_\n\t      /___|");
             } 
             else if(total_tries==3)
             {
                 printf("\n\t\t_______\n\t\t|     |\n\t\t|     O \n\t\t|    |T|\n\t\t|     \n\t\t|      \n\t\t|      \n\t\t|      \n\t       _|_\n\t      /___|");
             }
             else if(total_tries==2)
             {
                 printf("\n\t\t_______\n\t\t|     |\n\t\t|     O \n\t\t|    |T|\n\t\t|    /\n\t\t|      \n\t\t|      \n\t\t|      \n\t       _|_\n\t      /___|");
             }
             else if(total_tries==1)
             {
                 printf("\n\t\t_______\n\t\t|     |\n\t\t|     X \n\t\t|    |T|\n\t\t|    //\n\t\t|      \n\t\t|      \n\t\t|      \n\t       _|_\n\t      /___|");
             }
    
    
             
         printf("\n\n\nguessed letter: ");
             
         
         len=strlen(letter);
         undscr='_';
         printf("\n\n");
         printf("the word to guess is: ");
         for(loop=0;loop!=len;loop++)
         {
             printf(" %c", undscr);
         }
        printf("\nthe word to guess is: "); 
         for(j=0; j!=len;j++){
           printf(" %c", letter[j]);
         }
         
         printf("\n\nEnter a Letter(lowercase only): ");
         scanf("%c", letter_guess);
         
         getch();
         return 0;
         
    }
    my problem is:
    1)changing the blanks into a letters;
    2)when the inputted letter/s are wrong, the total_tries decrements and so the hangman;
    3)when the letter get doubled it says that the letter is already input and the total_tries decrements;
    4)when the total_tries reach "0" its game over, else if the word got completed print you win...


    i'm using dev c++...thanks in advance...

  2. #2
    Registered User
    Join Date
    Sep 2012
    Posts
    29
    sorry i'm new here...can anyone please help me with this, i got stuck with this and i don't know how to insert code with regards to my problem...

  3. #3
    qny
    qny is offline
    Registered User
    Join Date
    Sep 2012
    Posts
    355
    A() and B() are terribly awful names for functions.
    Try to come up with more descriptive names.

    Also you may get answers more quickly if you make isolated simple questions with little code.
    Putting 165 lines of code and telling us "I have 4 problems with the code" is not going to make any of us want to help, I think.

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    29
    Quote Originally Posted by qny View Post
    A() and B() are terribly awful names for functions.
    Try to come up with more descriptive names.

    Also you may get answers more quickly if you make isolated simple questions with little code.
    Putting 165 lines of code and telling us "I have 4 problems with the code" is not going to make any of us want to help, I think.
    sorry for this... i'm new here... T_T

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    2,570
    Hmmm, no offense, but this code does not seem to be planned out very well. I'm having trouble figuring out what is supposed to be happening by reading through the code.

    First, you seem to be missing a closing brace for the "while" loop in the "B()" function. (And since you can name your own functions, you should be giving them more meaningful names.)

    Second, you print the introduction twice in a row - once in main, and once when you call "A()".

    The variables in main are not used anywhere.

    Why do you hide the word as it is entered, but print the word to be found directly under the blank spots? (I hope it's for troubleshooting purposes.)

    You have the wrong syntax in the following line:

    Code:
    printf("\n\nEnter a Letter(lowercase only): ");
        scanf("%c", letter_guess);
    You want to assign this value to the address of "letter_guess" - which means, add an ampersand (&) in front of the variable name.

    Your "while" loop in "B()" will never loop because you "return 0" at the end of it.

    Which leads to the fact that you're not declaring the return type of your functions (char, void, etc).

    ---

    My recommendation: Start over. Take the program step by step. Consider each step a small project of its own. First, just write a program to display the introduction. Run it and verify it does what it is supposed to. Then add code to take input from the user (the word to be guess), and print it out to the screen. Compile and run it, and verify it's correct. Take it step by step.

    Read up on functions to get a better understanding.

    I'd also recommend reading up on following a sturdy development process.

    If you get stuck, come back and ask questions.

    Best of luck!

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    29
    Matticus, big thanks to you!...sorry about that, i'm new in programming...sorry for many errors...

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    2,570
    No problem. If you follow the "development process" link I put above, you'll find that your code development will go much smoother. The trick is to write code in small bits and make sure it compiles and does what it's supposed to before adding more code. Also, when you get into relatively sophisticated programs, spend a little time with a pen and paper before you write any code. This can help you visualize the overall structure and flow of the program, so you have something to work from.

  8. #8
    Registered User
    Join Date
    Sep 2012
    Posts
    29
    okay, thanks again... , i'll ask questions again if i got stuck...

  9. #9
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You need a better program structure. How about something like this:
    Code:
    Hangman pseudocode
    
    main:
        print_welcome()
        do:
            choose_word()
            guess_word()
        while want to play again
    
    
    guess_word:
        while hangman is not complete:
            guess_letter()
            if letter in word:
                show letter
                if whole word has been guessed:
                    print you win
                    return
            else:
                extend hangman
        print you lose
    patta and Hemerson like this.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hangman Problem
    By chixm8_49 in forum C Programming
    Replies: 16
    Last Post: 09-05-2009, 06:45 AM
  2. Hangman game problem
    By piradie in forum C Programming
    Replies: 9
    Last Post: 12-30-2008, 03:29 PM
  3. Replies: 9
    Last Post: 12-03-2006, 08:01 AM
  4. problem with my c++ project!!!
    By yamca in forum C++ Programming
    Replies: 4
    Last Post: 08-08-2006, 04:04 AM
  5. Project problem
    By GUI_XP in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2003, 01:57 PM

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