Thread: Reading input with fgets

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    14

    Unhappy Reading input with fgets

    Dear programmers,

    I have this problem in getting a user's input. I'll try to explain may problem as good as i can:

    - I'm using the struct to store some data:
    Code:
    typedef struct restaurant {
    	char name[NAMESIZE];
    	char email[EMAILSIZE];
    	char type[TYPESIZE];
    }Restaurant;
    - Then, fill this struct I ask the user to write some data, with this:

    Code:
    //Insert Name
    add_text("Name",&r,r->name,sizeof(r->name));
    	
    //Insert e-mail
    add_text("e-Mail",&r,r->email,sizeof(r->email));
    	
    //Insert restaurant type
    add_text("Type",&r,r->type,sizeof(r->type));
    - The add_text function is:

    Code:
    int add_text(const char* field, Restaurant** res, char* data, int size)
    {
        printf("%s: ", field);
        fflush(stdout);
        if (fgets(data, size, stdin) == NULL)
        {
            printf("\n<No data found!>\n");
            free(*res);
            return 1;
        }
        clean_string(data);
        return 0;
    }
    So the problem is with the fgets functions. The first time I use this last function my program seems to jump to the next add_text() function. So the result is:

    (prompt)
    "Name: e-Mail: kovitch@gmail
    Type: none"

    As you see it jumps somehow, and i don't have the opportunity to write the name down.

    What could be the problem here? I'd be very greatfull if somebody could help me out.

    Regards

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    probably - you have used scanf before fgets - which leaves \n in the stdin
    This \n is read by fgets

    Better - read all the input using fgets

    and why do you need to pass pointer to pointer to struct? you do not set it to NULL anyway...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    14
    Quote Originally Posted by vart View Post
    probably - you have used scanf before fgets - which leaves \n in the stdin
    This \n is read by fgets
    yes, you were right! Now there are no scanfs. It works for the first 4 times, so using:

    Code:
    //Insert Name
    add_text("Name",&r,r->name,sizeof(r->name));
    	
    //Insert e-mail
    add_text("e-Mail",&r,r->email,sizeof(r->email));
    	
    //Insert restaurant type
    add_text("Type",&r,r->type,sizeof(r->type));
    
    //Insert Recess
    add_text("Recess",r->recess,sizeof(r->recess));
    	
    //Insert Phone
    add_text("Phone",r->phone,sizeof(r->phone));
    
    //Insert Observations
    add_text("Observations",r->obs,sizeof(r->obs));
    will result in:

    (prompt)
    "Name: aaaa
    e-Mail: aaaa
    Type: aaaa
    Recess: aaaa
    Phone: Observations: "

    It jumps again. :S What could be the problem now?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Perhaps you typed more characters at Recess than it could take? fgets will stop at either a new line or when size-1 characters are read in.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by tabstop View Post
    Perhaps you typed more characters at Recess than it could take? fgets will stop at either a new line or when size-1 characters are read in.
    and you can check it by looking for the \n in the buffer,
    and if it is not there - read out the stdin till you find the \n
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    14
    It seems that you're right. Thanks for the tip

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fgets not getting user input
    By golgotha in forum C Programming
    Replies: 4
    Last Post: 03-14-2009, 11:27 AM
  2. Replies: 7
    Last Post: 02-02-2009, 07:27 AM
  3. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  4. Trouble with a lab
    By michael- in forum C Programming
    Replies: 18
    Last Post: 12-06-2005, 11:28 PM
  5. fgets - user input including spaces
    By MethodMan in forum C Programming
    Replies: 24
    Last Post: 03-12-2004, 07:36 PM