Thread: irritating bug wont go

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    6

    irritating bug wont go

    I cant explain why this is happening :

    Code:
         while(1)
            {
                                                                                                                                 
                    printf("what do you want to do \n");
                     printf("a> Send a file to the backend \n");
                     printf("b> Read a file in the backend \n");
                     printf("c> Execute a file in the backend \n");
                                                                                                                                 
                     scanf("%c",&choice);
                     printf("choice entered :%c\n",choice);
                      
                     printf("enter the name of the file \n");
                     ..
    .                ...
           }
    this program is supposed to be in an infinite loop.

    After the first loop is completed properly , the three options are displayed and the user is supposed to make his next choice.
    But, my program is skipping through the scanf function in the second loop.
    The first iteration is as expected,
    this is what it shows after the first iteration :

    Code:
    OUPUT:
    
    
    a> Send a file to the backend
    b> Read a file in the backend
    c> Execute a file in the backend
    choice entered :
     
    enter the name of the file
    Even before i type anything.
    in my code, I compare choice to select the option required, it doesnt get changed anywhere.

    Why is this happening?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > But, my program is skipping through the scanf function in the second loop.
    No, having typed in "a" and "enter", the first scanf("%c") call reads the 'a' and the second one reads the '\n'.

    Use fgets to read a whole line of input, then examine that line in memory
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    6
    phew!! never knew that was happening..
    I have a doubt in your method . OK, i will do:
    Code:
          fgets(&choice,sizeof(char), stdin);
    fine, what did you mean by check the contents in memory??

    in the mean time i had tried this :

    Code:
      char  *choice;
         while(1)
            {
                    choice=malloc(sizeof(char));
                     printf("what do you want to do \n");
                     printf("a> Send a file to the backend \n");
                     printf("b> Read a file in the backend \n");
                     printf("c> Execute a file in the backend \n");
                                                                                                                                 
                    scanf("%c",choice); 
                    printf("choice entered :%c\n",*choice);
                     printf("enter the name of the file \n");
                     ..
                     ..
    
    
                       ..
                      ..
                      free(choice);
             }
    I thought this would work, but it didnt either..
    Can you elaborate your idea a little bit..

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    204
    Try this:
    Code:
    scanf("%c%*c",&choice);

  5. #5
    Registered User
    Join Date
    Jun 2006
    Posts
    6
    well, that doesnt do the job either

    this is the output now :

    Code:
    what do you want to do
    a> Send a file to the backend
    b> Read a file in the backend
    c> Execute a file in the backend
    a
    choice entered :
     
    enter the name of the file
    Marginally better, as it is waiting for the input from the keyboard, but the choice variable isnt getting updated.

    what did you imply anyway??

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    More spoon feeding
    Code:
    while(1)
    {
      char buff[BUFSIZ];
      printf("what do you want to do \n");
      printf("a> Send a file to the backend \n");
      printf("b> Read a file in the backend \n");
      printf("c> Execute a file in the backend \n");
      fgets( buff, BUFSIZ, stdin );
      printf("choice entered :%c\n",buff[0]);
      printf("enter the name of the file \n");
      /* more fgets calls */
    }
    If you just invented a char * variable from looking at the prototype of fgets() and saw a char*, then you need to read the books again.
    It is not sufficient to simply declare a bunch of variables with the right type, you have to think about what is going to happen (by reading the manual)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Jun 2006
    Posts
    6
    Ofcourse, I know what fgets() does and how it works.
    and by the way, your idea still didnt work.
    it is still taking in the '\n' as the input to the scanf() , in the second iteration and jumping into the next statement..

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Stop mixing your methods of reading input. He didn't say "use fgets then use scanf". He said...
    Code:
    /* more fgets calls */
    See what happens when you actually pay attention?


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Jun 2006
    Posts
    6
    oh! sorry, i just typed that carelessly..my fault.

    i should have typed "fgets()" instead of "scanf().."

  10. #10
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Quote Originally Posted by nash@nitk
    Ofcourse, I know what fgets() does and how it works.
    and by the way, your idea still didnt work.
    it is still taking in the '\n' as the input to the scanf() , in the second iteration and jumping into the next statement..
    Then you did it wrong. fgets() can't leave the \n in the buffer for the next fgets() if you used it right -- and Salem used it right. Therefore, you probably didn't.

    Maybe you need to post the code that doesn't work....
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gaks bug?
    By Yarin in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 08-31-2008, 02:47 PM
  2. Debugging a rare / unreproducible bug..
    By g4j31a5 in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 08-05-2008, 12:56 PM
  3. Bug in iterator comparison in C++ standard?
    By steev in forum C++ Programming
    Replies: 14
    Last Post: 07-12-2008, 12:02 AM
  4. ATL bug of CComPtr?
    By George2 in forum Windows Programming
    Replies: 6
    Last Post: 04-07-2008, 07:52 AM
  5. wierdest (annoying) C++ bug
    By phantom in forum C++ Programming
    Replies: 3
    Last Post: 10-20-2001, 08:12 PM