Reading input with fgets

This is a discussion on Reading input with fgets within the C Programming forums, part of the General Programming Boards category; Dear programmers, I have this problem in getting a user's input. I'll try to explain may problem as good as ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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,185
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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, 06: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, 10:28 PM
  5. fgets - user input including spaces
    By MethodMan in forum C Programming
    Replies: 24
    Last Post: 03-12-2004, 06:36 PM

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