fgets() and structures issue

This is a discussion on fgets() and structures issue within the C Programming forums, part of the General Programming Boards category; I am having a problem with fgets() waiting for stdin. Any help is appreciated. Code: #include <stdio.h> struct myPIM { ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    3

    fgets() and structures issue

    I am having a problem with fgets() waiting for stdin. Any help is appreciated.

    Code:
    #include <stdio.h>
    
    struct myPIM
    {
    
       char firstname[15];
       char lastname[20];
       char homeaddr[30];
       char city[30];
       char state[3];
       long int zip;
    
    };
    
    
    main()
    {
       struct myPIM person1;
    
       printf("Person # 1\n");
       printf("Firstname: ");
       scanf("%s", person1.firstname);
       printf("Lastname: ");
       scanf("%s", person1.lastname);
       printf("Street Address: ");
       fgets(person1.homeaddr, 30, stdin);
       printf("City: ");
       scanf("%s", person1.city);
       printf("State: ");
       scanf("%s", person1.state);
       printf("Zipcode: ");
       scanf("%ld", &person1.zip);
    
    
       /* print the entered information */
       printf("\n\n*** Your Personal Information ***\n");
       printf("Firstname: %s\n", person1.firstname);
       printf("Lastname: %s\n", person1.lastname);
       printf("Street Address: %s\n", person1.homeaddr);
       printf("City: %s\n", person1.city);
       printf("State: %s\n", person1.state);
       printf("ZipCode: %ld\n", person1.zip);
    }

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    scanf leaves newlines in the stream, causing fgets to pick it up and think it's done. Call fgetc(stdin) to eat the newline.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    3

    Thumbs up

    cool thanks for the fast reply. I appreciate it.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,767
    Or better yet, use

    Code:
    char buff[BUFSIZ];
    fgets( buff, sizeof buff, stdin );
    To read each line of input.

    When you've validated the data, then convert / copy / whatever the data into your struct.

    Ad-hoc reading directly into the struct without validation is no way to go.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Evaluation of structure's element against input not working!
    By laczfinador in forum C Programming
    Replies: 6
    Last Post: 05-11-2009, 02:19 PM
  2. Problem with arrays structures sorting.
    By pitifulworm in forum C Programming
    Replies: 42
    Last Post: 02-09-2009, 12:31 PM
  3. Inserting values into Structures
    By aligeb in forum C Programming
    Replies: 7
    Last Post: 05-02-2007, 09:35 AM
  4. Data Structures Warning
    By jlharrison in forum C Programming
    Replies: 6
    Last Post: 04-08-2006, 01:04 AM
  5. problem with fgets
    By Smoot in forum C Programming
    Replies: 4
    Last Post: 12-07-2003, 03:35 AM

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