Guess word Problem

This is a discussion on Guess word Problem within the C Programming forums, part of the General Programming Boards category; hello there, i got problem with my code, when i starting to guess a letter and hit the enter, the ...

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

    Guess word Problem

    hello there, i got problem with my code, when i starting to guess a letter and hit the enter, the letter i've guess is not printing in the blank i made. can someone help me to fix the code? thanks.

    Code:
    int main(){
        int i;
        int j;
        int x=0;
        char word[30];
        char undscr[30];
        char length;
        char letter;
        
        printf("enter a word: ");
        gets(word);
        
        length=strlen(word);
        undscr[x]='_';
        for(i=0;i<length;i++)
        {
            printf(" %c", undscr[x]);
        }
        
        printf("\n\nguess the letter: ");
        scanf("%c", &letter);
        for(j=0;j<i;j++)
        {
            if(letter==word[i])
            {
                undscr[i]=word[i];
            }
        }
        
        getch();
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2012
    Posts
    29
    reply for this please, i really need your help.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Try this:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <conio.h>
     
     int main(){
       int i;
       int done=0;
       int good=0;
       char word[30];
       char undscr[30];
       char length;
       char letter;
        
       _gotoxy(1,3);
       printf("enter a word: ");
       gets(word);
        
       length=strlen(word);
       for(i=0;i<length;i++) {
          undscr[i]='_';
       }
       while(!done) {
          _gotoxy(1,5);
          for(i=0;i<length;i++)
          {
             printf("%c ", undscr[i]);
          }
          if(good>=length) {
             done=1;
             break;
          }
    
          printf("\n\nguess the letter: ");
          letter=_getch();
          printf("%c",'\b');
          
          for(i=0;i<length;i++)
          {
             if(letter==word[i])
             {
                undscr[i]=word[i];
                good++;
             }
          }
       }
       _gotoxy(1,9);
       if(done)
          printf("You guessed it!\n\n");
       printf("Hit Enter When Ready\n");
       getchar(); 
       return 0;
    }

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    29
    Quote Originally Posted by Adak View Post
    Try this:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <conio.h>
     
     int main(){
       int i;
       int done=0;
       int good=0;
       char word[30];
       char undscr[30];
       char length;
       char letter;
        
       _gotoxy(1,3);
       printf("enter a word: ");
       gets(word);
        
       length=strlen(word);
       for(i=0;i<length;i++) {
          undscr[i]='_';
       }
       while(!done) {
          _gotoxy(1,5);
          for(i=0;i<length;i++)
          {
             printf("%c ", undscr[i]);
          }
          if(good>=length) {
             done=1;
             break;
          }
    
          printf("\n\nguess the letter: ");
          letter=_getch();
          printf("%c",'\b');
          
          for(i=0;i<length;i++)
          {
             if(letter==word[i])
             {
                undscr[i]=word[i];
                good++;
             }
          }
       }
       _gotoxy(1,9);
       if(done)
          printf("You guessed it!\n\n");
       printf("Hit Enter When Ready\n");
       getchar(); 
       return 0;
    }
    [Linker error] undefined reference to `gotoxy' , im using dec c++ as a compiler.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Remove the underscore from in front of _gotoxy's. gotoxy() is part of conio.h, but my compiler uses the leading underscore for it nowadays.

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    29
    Quote Originally Posted by Adak View Post
    Remove the underscore from in front of _gotoxy's. gotoxy() is part of conio.h, but my compiler uses the leading underscore for it nowadays.
    its working thanks!!...

  7. #7
    Registered User
    Join Date
    Sep 2012
    Posts
    29
    but its only works in TurboC not in DevC++.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Does Dev C support the conio.h include file? Do you have a conio.h file in your header file directory?

    I don't have Dev C, so i can't help you with those problems.

    If you are using Windows, you can use SetConsoleCursorPosition(), (you may have to include "windows.h"), instead of gotoxy(), but I'm not sure how to get a key stroke like getch(), in Windows. You can also skip the gotoxy() and getch(), and make it either a "rolling" screen presentation of the game input and output, or clear the screen between letters being entered. That will give the game a bit of a "blinking" look, but it might be OK.

    I have heard that NCurses supports a lot of the same functions that conio.h does, but I haven't used it myself.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 28
    Last Post: 10-23-2011, 07:17 PM
  2. Replies: 17
    Last Post: 10-20-2011, 06:32 PM
  3. Replies: 7
    Last Post: 10-19-2011, 08:45 AM
  4. MS Word Problem
    By golfinguy4 in forum Tech Board
    Replies: 8
    Last Post: 06-22-2003, 01:33 AM
  5. hows life fine i guess.Well i have a problem again
    By datainjector in forum C Programming
    Replies: 3
    Last Post: 07-02-2002, 11:48 PM

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