Thread: Help me please (correcting some command errors)

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

    Help me please (correcting some command errors)

    when i always run the program then started to input a correct letter, the total_tries always trigger to decrements by 1 and when i am going to input a wrong letter it is decrements by 2...i cant find what is the problem. can anyone help me for my problem? thanks alot, sorry for the long code...

    Code:
       gotoxy(1,3);   printf("enter a word: \n");
       do{
             c=getch();
             switch(c)
             {
                 case '\r':
                      break;
                 case '\b':
                      printf("\b \b");
                      --x;
                      break;
                 default:
                      printf("%c", '*');
                      word[x++]=c;
                      break;
             }
         }while(c != '\r');
         word[x]='\0';
         
         clrscr();
         
         
         length=strlen(word);
         for(i=0;i<length;i++)
         {
             undscr[i]='_';
         }
                      
         while(!done&&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|     O \n\t\t|    |T|\n\t\t|    //\n\t\t|      \n\t\t|      \n\t\t|      \n\t       _|_\n\t      /___|");
             }
             
             
             
             gotoxy(1,15);
             for(i=0;i<length;i++)
             {
                 printf("%c ", undscr[i]);
             }
         
             if(good>=length)
             {
                 done=1;
                 break;
             }
    
    
         
         
         
         
         do{
         printf("\n\n\n\nguess a letter: ");
         letter=getchar();
         for(i=0;i<length;i++)
         {
    
    
             if(letter==word[i])
             {
                undscr[i]=word[i];
                good++;
                disc=1;
             }
             
             
             
             
             
          clrscr();
          }
          
          if(disc==0)
          {
             total_tries--;
          }
          disc=0;
          
          }
         while(i!=length && disc!=0);     
    
    
          }
       
       if(done)
          gotoxy(1,20); 
          printf("You guessed it!\n\n");
          printf("Hit Enter to exit\n");
       
       getch();
       return 0;
    }

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    You understand that this loop has no body
    Code:
    while(i!=length && disc!=0);
    -maybe irrelevant to the problem

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by std10093 View Post
    You understand that this loop has no body
    Code:
    while(i!=length && disc!=0);
    -maybe irrelevant to the problem
    That loop is fine. It's the bottom half of a do-while loop that starts on line 82.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    @Hemerson:
    First off, you could use some better indentation, it's hard to follow your code. If your code is messy, it means you're more likely to make mistakes and it's harder to find and fix them. Also, it would help if you provided complete code, because I don't know how you declared your variables, so you may have problems related to that too, and I can't put your code in my compiler to check for basic errors that way either.

    I'm guessing your problem with decrementing total_tries is the new line. You type a letter, then press enter. That enter is put in the input buffer (as a '\n'), just like the letter you typed, so getchar will read it. Since the newline is not in the word you're guessing, so it acts like you guessed an incorrect letter. I would try something like:
    Code:
    letter = getchar();
    if (letter == '\n')
        continue;  // don't process the new line, let the user enter another character
    Also, I think your if statement around line 113 is missing a set of curly braces.

  5. #5
    Registered User
    Join Date
    Sep 2012
    Posts
    29
    @anduril462 thank you very much, its working...

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    29
    @anduril462, there is another error that i can't fix...the repitition of letter...when i enter again a letter, at first its ok...but entering again and again the letter, it automatically saying that "i guess it press enter to exit", how to fix it?, thanks...

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I'd like to see your for loops changed. Instead of i=0;i<length;i++, I'd like to see good=0;good<length;no increment here. Instead, increment good only when it has matched another letter in the word being guessed. (And increment it by two, if the letter is matching a letter that is repeated in the word being guessed at, etc.)

    You have loops, that should be consolidated to make the code easier and shorter. And please, get your indentation up to snuff.

  8. #8
    Registered User
    Join Date
    Sep 2012
    Posts
    29
    Quote Originally Posted by Adak View Post
    I'd like to see your for loops changed. Instead of i=0;i<length;i++, I'd like to see good=0;good<length;no increment here. Instead, increment good only when it has matched another letter in the word being guessed. (And increment it by two, if the letter is matching a letter that is repeated in the word being guessed at, etc.)

    You have loops, that should be consolidated to make the code easier and shorter. And please, get your indentation up to snuff.

    i did'nt get it clearly, sorry sir. can you give me some examples?. thanks.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    There are two other guessing game programs that have been on the forum here, in the last week, check them out for an example.

  10. #10
    Registered User
    Join Date
    Sep 2012
    Posts
    29
    @adak, how to increment by two?...

    :::updated:::

    i got it...hehehe, thanks.
    Last edited by Hemerson; 10-07-2012 at 08:11 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. command line inputs causing strange errors, what's going on?
    By Hear.Me.ROAR in forum C Programming
    Replies: 25
    Last Post: 07-11-2011, 07:07 PM
  2. Reading and correcting.
    By Remm in forum C Programming
    Replies: 3
    Last Post: 02-27-2008, 04:13 PM
  3. please,helpe me in correcting this code!!
    By sara* in forum C++ Programming
    Replies: 4
    Last Post: 01-26-2006, 02:07 AM
  4. Correcting a file?
    By Kings in forum C++ Programming
    Replies: 3
    Last Post: 03-14-2003, 04:18 PM
  5. correcting my last thread...
    By demonus in forum C Programming
    Replies: 3
    Last Post: 11-04-2002, 12:48 PM