what is wrong with the input?

This is a discussion on what is wrong with the input? within the C Programming forums, part of the General Programming Boards category; hi, I am a new lover of C,I have recently written the code below, it is a program to guess ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    21

    Question what is wrong with the input?

    hi, I am a new lover of C,I have recently written the code below, it is a program to guess the number.
    Code:
    #include<stdio.h>
    int main(void)
    {
        
        int a;
        char response;
        
      printf("pls pick up a no from 1 to 100:");
      printf("is your no 50?");
      if((response=getchar())=='l')
      {
      a=75;
      printf("is your no %d",a);
                 while((response=getchar())!='y')
                 {
                  if((response=getchar())=='h')
                  {
                  a=50+(a-50)/2;
                  printf("is your no %d",a);
                  }
                   else
                   {
                   a=a+(100-a)/2;
                   printf("is your no %d",a);
                   }
                    }
                     printf("I got it,haha!");
                               }
                               else if((response=getchar())=='h')
                               {
                               a=25;
                               printf("is your no %d",a);
                                          while((response=getchar())!='y')
                                          {
                                              if((response=getchar())=='h')
                                              {
                                              a=(a-1)/2;
                                              printf("is your no %d",a);
                                              }
                                               else
                                               {
                                               a=a+(50-a)/2;
                                               printf("is your no %d",a);
                                                          }
                                                               }
                                                               printf("I got it,haha!");
                                                                         }
                                                                          else
                                                                                                                                                                          
                                                                          printf("I got it, haha!");
                                   
                                  
    system("pause");
    return 0;
    }
    first I typed in "l", then it prompted me with "is your no 75", I typed in "y" right after that, and what I got was "is your no 87". but it should print out "I got it, haha!" from the logic of the program. I tried typing a space before typing "y", and it gave me another guess with "I got it,haha!" (two sentence together). can anybody tell me what was wrong? Thanks.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,171
    Good lord. What did you do with your indentation? Lining everything up in a reasonable manner would be a good first step to figuring out what's wrong (and getting other people more apt to look at your code).
    If you understand what you're doing, you're not learning anything.

  3. #3
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    A present for the OP...

    Code:
    #include<stdio.h>
    
    int main(void)
    {
    
        int a;
        char response;
    
        printf("pls pick up a no from 1 to 100:");
        printf("is your no 50?");
        if ((response = getchar()) == 'l') {
            a = 75;
            printf("is your no %d", a);
            while ((response = getchar()) != 'y') {
                if ((response = getchar()) == 'h') {
                    a = 50 + (a - 50) / 2;
                    printf("is your no %d", a);
                } else {
                    a = a + (100 - a) / 2;
                    printf("is your no %d", a);
                }
            }
            printf("I got it,haha!");
        } else if ((response = getchar()) == 'h') {
            a = 25;
            printf("is your no %d", a);
            while ((response = getchar()) != 'y') {
                if ((response = getchar()) == 'h') {
                    a = (a - 1) / 2;
                    printf("is your no %d", a);
                } else {
                    a = a + (50 - a) / 2;
                    printf("is your no %d", a);
                }
            }
            printf("I got it,haha!");
        } else
    
            printf("I got it, haha!");
    
        return 0;
    }
    Like itsme86 said - your indenting is bad news.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,171
    It looks like your problem is you're forgetting the newline character. When you type "y<ENTER>" the first getchar() will grab the y and the next getchar() will grab the '\n' from pressing ENTER.

    Now say "thank you, kermit".
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    21

    Smile thank you

    I see, thanks for you guys to help me, sorry for the bad indentation, I should improve on that also.
    I did a review of my code, and came out the code here:
    Code:
    #include<stdio.h>
    int main(void)
    {
        
        int a;
        char response;
        
      printf("pls pick up a no from 1 to 100:");
      printf("is your no 50?");
      if((response=getchar())=='l')
      {
      printf("%c",response);
      a=75;
      printf("is your no %d",a);
       while(getchar()!='\n')
       continue;
     
        while((response=getchar())!='y')
        {
         if(response=='h')
         {
         a=50+(a-50)/2;
         printf("is your no %d",a);
         while(getchar()!='\n')
         continue;
         }
         else
         {
         a=a+(100-a)/2;
         printf("%c",response);
         printf("is your no %d",a);
         while(getchar()!='\n')
         continue;
         }
        }
        printf("I got it,haha!");
       }
       else if((response=getchar())=='h')
       {
       a=25;
       printf("is your no %d",a);
       while(getchar()!='\n')
       continue;
       while((response=getchar())!='y')
        {
        if(response=='h')
         {
         a=(a-1)/2;
         printf("is your no %d",a);
         while(getchar()!='\n')
         continue;
         }
         else
         {
         a=a+(50-a)/2;
         printf("is your no %d",a);
         while(getchar()!='\n')
         continue;
         }
        }
        printf("I got it,haha!");
       }
       else
       printf("I got it, haha!");
    system("pause");
    return 0;
    }
    it is working when I first gave the answer "low". but it gives me "I got it, haha!" after I keyed in "high"(it should take a guess of 25 instead).
    I cannot find anything wrong with the conditonal statements
    Last edited by timhxf; 08-12-2006 at 11:13 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem grabbing k/b input
    By falcon9 in forum C Programming
    Replies: 2
    Last Post: 10-28-2007, 12:47 PM
  2. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 04:42 PM
  3. How can I repeat a menu on wrong input?
    By Guti14 in forum C Programming
    Replies: 5
    Last Post: 10-21-2004, 11:21 PM
  4. Replies: 9
    Last Post: 07-15-2004, 04:30 PM

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