Hangman Project Problem

This is a discussion on Hangman Project Problem within the C Programming forums, part of the General Programming Boards category; Hi there. i got stuck with this code. i can't get the right code for the loop to put the ...

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

    Hangman Project Problem

    Hi there. i got stuck with this code. i can't get the right code for the loop to put the letter into the blank. Can anyone help me here.
    thanks a lot.

    Code:
    printf("enter a word to guess: \n");     do{
               c=getch();
               switch(c)
               {
                   case '\r':
                        break;
                   case '\b':
                        printf("\b \b");
                        if(x>0) --x;
                        break;
                   default:
                           printf("%c", '*');
                           word[x++]=c;
                           break;
               }
            }while(c != '\r');
            word[x]='\0';
          
          system("cls");
            
         length=strlen(word);
         undscr[x]='_';
         printf("\n\nthe word to guess is: ");
         for(undrscr=0;undrscr!=length;undrscr++)
         {
             printf(" %c", undscr[x]);
         }
         
         printf("\n\nguess a letter: ");
         scanf("%s", &guess_letter[x]);

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,343
    Look for '\n' instead of '\r'
    Fact - Beethoven wrote his first symphony in C

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    29
    can someone help me to add a code to make my inputted letter put in the blank. thanks in advance.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    What problem are you having putting a letter where you need it?

    You can always "walk" through the string, and test each char in the string for what you need to find, then assign the new char into that position of the string.

    You will have to be more specific, because I can't understand what the problem is.

  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    29
    @adak, here is my lay out example:

    type a word: *** <----this is the ouput of the code i post above. (dog)

    the word to guess is: _ _ _

    enter a letter: d
    ===================================

    the problem is when i input the letter "d" it didnt appear in the blank...can you help me with my problem...thanks...

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Hemerson View Post
    @adak, here is my lay out example:

    type a word: *** <----this is the ouput of the code i post above. (dog)

    the word to guess is: _ _ _

    enter a letter: d
    ===================================

    the problem is when i input the letter "d" it didnt appear in the blank...can you help me with my problem...thanks...

    If the cursor is on another line of the screen, then it will need to be repositioned to right after the :

    And the easiest way to do that, is to use conio.h as one of the include files. (Ncurses is another option)

    Do you have conio.h on your compiler? It is not standard C, but most compilers support it, just for this purpose.

  7. #7
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,343
    You need to make all of undrscr '_' -> I think that using the function memset from string.h would be the best way to do that


    After you get a guess, go through 'word' character by character. When you find a match (say in word[2]) replace the corresponding character in undrscr with the guess (undrscr[2]). Clear the screen and reprint everything.


    I'm guessing that you already are using conio.h, because you are using the function "getch()".
    Fact - Beethoven wrote his first symphony in C

  8. #8
    Registered User
    Join Date
    Sep 2012
    Posts
    29
    i can't get your point, but i include stdio.h, conio.h, stdlib.h...can you give more explanation...

  9. #9
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,343
    What do you not understand?
    Fact - Beethoven wrote his first symphony in C

  10. #10
    Registered User
    Join Date
    Sep 2012
    Posts
    29
    Quote Originally Posted by Click_here View Post
    What do you not understand?
    sorry its for adak not for you click_here. sorry for the misunderstand.

  11. #11
    Registered User
    Join Date
    Sep 2012
    Posts
    29
    @click_here how? can you give me some code example?

  12. #12
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,343
    I think that it won't benefit you if I just gave it to you

    for each element in word
    if the element is the same as the guess then
    make the same element in the underscore array the guess

    Think about it - You can get it!
    Fact - Beethoven wrote his first symphony in C

  13. #13
    Registered User
    Join Date
    Sep 2012
    Posts
    29
    Quote Originally Posted by Click_here View Post
    You need to make all of undrscr '_' -> I think that using the function memset from string.h would be the best way to do that


    After you get a guess, go through 'word' character by character. When you find a match (say in word[2]) replace the corresponding character in undrscr with the guess (undrscr[2]). Clear the screen and reprint everything.


    I'm guessing that you already are using conio.h, because you are using the function "getch()".

    Sir. what is function memset of string.h?

  14. #14
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,343
    It's used like this:

    Code:
    memset (void *dest, int c, size_t count)
    where dest is your array
    c is the character that you want it to change to
    count is the number of characters you want to change
    
    example
    char arr[50];
    memset (arr, '\0', sizeof(arr));
    
    will set all elements in the array to the '\0' character
    
    char arr[50];
    memset (arr, '_', sizeof(arr));
    
    will set all elements in arr to the '_' character
    "Maybe, just once, someone will call me "sir" without adding, 'you're making a scene.'"
    -Homer J Simpson
    Last edited by Click_here; 10-02-2012 at 11:37 PM.
    Fact - Beethoven wrote his first symphony in C

  15. #15
    Registered User
    Join Date
    Sep 2012
    Posts
    29
    Okay i got it! thanks! i'll be back when i got stuck again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hangman Project Problem
    By Hemerson in forum C Programming
    Replies: 8
    Last Post: 10-01-2012, 11:10 AM
  2. Hangman Problem
    By chixm8_49 in forum C Programming
    Replies: 16
    Last Post: 09-05-2009, 06:45 AM
  3. Hangman game problem
    By piradie in forum C Programming
    Replies: 9
    Last Post: 12-30-2008, 03:29 PM
  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