Thread: unusual behaviour by GETS function

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    21

    Unhappy unusual behaviour by GETS function

    The first gets function gets skiped but the remaining get functions work, that is the control just moves on to the next line. WHy is this happenning


    Code:
    void add_address(void)
    {
        int slot;
        slot = -1;
        
        slot = find_free();
        
        if( slot == -1 )
        {
            printf("\nData base full!!!! Cannot add record");
            return;
        }
        
        printf("\nAdding record to database at slot number : %d \n", slot);
        
        printf("\nEnter Name : ");
        gets(address[slot].name);               //<=THIS gets() gets skiped
        
        printf("\nEnter Street : ");
        gets(address[slot].street);              //BUT this gets() works, why???
        
        printf("\nEnter Citi : ");
        gets(address[slot].citi);
        
        printf("\nEnter state : ");
        gets(address[slot].state);
        
        printf("\nEnter ZIP : ");
        scanf("%u", &address[slot].zip);
        
        return;
        
    }
    When I run this program this part of the code
    Code:
        printf("\nEnter Name : ");
        gets(address[slot].name);
    gets neglected, control just moves on to the nex line and asks for the input for the next data which is
    Code:
        printf("\nEnter Street : ");
        gets(address[slot].street);
    I cannot understand why its not asking for a input for
    Code:
        printf("\nEnter Name : ");
        gets(address[slot].name);
    , why is it moving on to next line.

    If I replace "gets" with "scanf", then the compiler asks for the input of name but will skip the next gets function.
    Last edited by capvirgo; 02-07-2008 at 02:10 AM.

  2. #2
    Registered User
    Join Date
    Feb 2008
    Posts
    21
    Declaration of address:
    Code:
    #define MAX 100
    
    struct  addr
    {
        char name[80];
        char street[40];
        char citi[40];
        char state[40];
        unsigned long int zip;
    }address[MAX];

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    The fisrt gets function gets skiped but the remaining get functions work, that is request for input. WHy is this happenning
    Probably because there is already something in the input buffer which gets() picks up. Also, you should use fgets() instead of gets() to prevent buffer overflows.

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    21
    When I step through the program the value of the first gets() remain as '\\0' unchanged , so its not picking up any thing from the input buffer

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    gets and scanf are a horrible input reading strategy. Your gets call does work, except that it reads the newline character from the stdin stream. scanf simply ignores the whitespace it reads, it can't discard it. If scanf didn't put back characters that failed to meet a format like "&#37;d" that may have a future impact on how the file was read later on.

    Proof of concept:
    Code:
    #include <stdio.h>
    
    int main ( void )
    {
        int a;
        char junk[100];
    
        if ( scanf( "%d", &a ) == 1 )
            gets( junk );
        if ( junk[0] == '\0' ) /**gets overwrites '\n' with a terminating zero**/
            puts( "woops! read the end of a line" );
    
        return 0;
    }
    The gets function has its own problems of course.

  6. #6
    Registered User
    Join Date
    Feb 2008
    Posts
    21
    thats right, thanks. But I added fflush(stdin) just before the gets(), but it still was reading newline character from previous input. Was fflush not flushing stdin buffers ?

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613

  8. #8
    Registered User
    Join Date
    Feb 2008
    Posts
    21
    yes, just found out that. what would be a safe way to read lines from keyboard

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by capvirgo View Post
    yes, just found out that. what would be a safe way to read lines from keyboard
    The best way is to make your own little function that does something like this:
    Code:
    #include <stdio.h>
    
    int readline(char *buf, size_t buflen)
    {
        fflush(stdout);    // Not needed in newer compilers, but just in case. 
        if (fgets(buf, buflen, stdin)
        {
            int len = strlen(buf);
            // Now check that we got a whole thing or if there's "stuff left behind".
            if (buf[len-1] != '\n')  {
                int ch;
                while ((ch = getchar()) != '\n' && ch != EOF)   /* do nothing */ ;
            }
            return 1;
        }
        return 0;    /* End of file indication */
    }
    
    
    int main()
    {
        char yesno[2];
        char name[20];
    
        printf("Answer y(es) or n(o):");
        if (!readline(yesno, sizeof(yesno))) return 1;   // Application failed if EOF here!
        if (stricmp(yesno, "y")) 
        {
           printf("What's your name?");
           if (!readline(name, sizeof(name))) return 1;  // App. failed... 
           printf("Hello %s\n", name);
        }  
        return 0; 
    }
    I haven't compiled that, but I think it shows the general idea.
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Feb 2008
    Posts
    21
    Great, Thanks all

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM