Don't know if this is a struct, a loop, or an fgets issue.

This is a discussion on Don't know if this is a struct, a loop, or an fgets issue. within the C Programming forums, part of the General Programming Boards category; I am currently a student learning c, and I had to make a struct loop to collect info for 10 ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    28

    Don't know if this is a struct, a loop, or an fgets issue.

    I am currently a student learning c, and I had to make a struct loop to collect info for 10 customers.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define NAME_LEN 10
    
    
    int main() {
    
        typedef struct
        {
            char firstName[30];
            char lastName[30];
            char street[35];
            char city[20];
            char state[3];
            int zip [15];
            char phone[15];
            int accountId[40];
        } Customer;
    
        int myIntArray[NAME_LEN] = {0};
        Customer custArray[NAME_LEN];
        int i = 0;
    
        for(i = 0; i < NAME_LEN; ++i)
        {
            printf("Enter Information for Customer %d\n", i + 1);
            printf ("First Name: ");
            fgets (custArray[i].firstName, 30, stdin);
            printf ("Last Name: ");
            fgets (custArray[i].lastName, 30, stdin);
            printf ("Address: ");
            fgets (custArray[i].street, 35, stdin);
            printf ("City: ");
            fgets (custArray[i].city, 20, stdin);
            printf ("State Code (ex. CO for Colorado, OR for Oregon): ");
            fgets (custArray[i].state, 3, stdin);
            printf ("Zip Code: ");
            fgets(custArray[i].zip, 15, stdin);
            printf("Phone Number: ");
            fgets (custArray[i].phone, 15, stdin);
            printf("Account Number: ");
            fgets (custArray[i].accountId, 40, stdin);
        }
        return(0);
    }
    It mostly works, but I'm having a really weird issue. If I enter more than a single character in the "State Code (ex. CO for Colorado, OR for Oregon): " entry, the next thing that pops up is

    Zip Code: Phone Number:

    It works fine if I just press enter or enter a single character, but not 2+. I realize I can just increase the array size to 4, but I'd like to know why this is happening. Shouldn't an array of 3 be capable of holding the two characters and the '\n'?

  2. #2
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    383
    Your "state" structure member has space for only 3 characters. Two of those are needed for the newline character (if present) and null terminator. So you can fit only a single character, or two characters if a newline is not present.

    Edit: hmm, maybe I should clarify a bit. When fgets reads two characters from stdin, it stops reading. The next fgets reads the newline from stdin, prematurely ending that string.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Posts
    28
    Ahhhhh, I forgot about a space for the '/0' as well. Silly me!

    Thank you! I have to say, this forum has been a lifesaver.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Possible fgets issue
    By Layvian in forum C Programming
    Replies: 7
    Last Post: 05-23-2012, 12:32 AM
  2. Facing issue with fgets in while loop
    By pingloo02 in forum C Programming
    Replies: 4
    Last Post: 04-17-2012, 07:14 AM
  3. Storing using fgets issue
    By ccc123 in forum C Programming
    Replies: 3
    Last Post: 05-30-2011, 12:41 PM
  4. fgets issue
    By Fox101 in forum C Programming
    Replies: 2
    Last Post: 05-05-2008, 11:11 PM
  5. fgets() and structures issue
    By gettyUP in forum C Programming
    Replies: 3
    Last Post: 12-21-2006, 11:26 AM

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