Thread: HELP! Strucutres!!

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    12

    Exclamation HELP! Strucutres!!

    I have this assignment, and the teacher said that it is open internet, open book, etc. I cannot figure out how to do the last part!

    1.Create an array that contains ten (10) variables of the following type:

    insert
    Code:
    typedef struct {
        char firstName[30];
        char lastName[30];
        char street[35];
        char city[20];
        char state[3];
        int zip;
        char phone[15];
        int accountId;
    } Customer;
    2.
    Prompt the user for values for each of the variables in each structure, i.e. you will be asking for 10 firstName values, 10 lastName values, and so forth. Hint: You should think about creating a function to input values for a structure, and then create a loop to call this function 10 times.

    3.
    After all of the values for the structure variables have been input, you need to prompt the user for a state string, and then display the values of all variables in each structure where the state string matches the user's input. Hint: You should think about creating a function to print all of the values for a structure, and then call the function for those structures that meet your criteria.

    You can assume that the user always enters proper data - no input validation is required.


    Sample Output

    Sample (user input in blue, program output in magenta)

    Enter Data for Customer 0
    Enter First Last Phone: Doug Oregon 123-456-7890
    Enter Address (Street City State ZIP): Main Portland OR 12345


    Enter Data for Customer 1
    Enter First Last Phone: Doug Washington 123-456-7890
    Enter Address (Street City State ZIP): Main Portland WA 12345


    Enter Data for Customer 2
    Enter First Last Phone: Doug California 123-456-7890
    Enter Address (Street City State ZIP): Main Portland CA 12345


    Enter Data for Customer 3
    Enter First Last Phone: Doug Nevada 123-456-7890
    Enter Address (Street City State ZIP): Main Portland NV 12345


    Enter Data for Customer 4
    Enter First Last Phone: Doug Colorado 123-456-7890
    Enter Address (Street City State ZIP): Main Portland CO 12345


    Enter Data for Customer 5
    Enter First Last Phone: Another Colorado 123-456-7890
    Enter Address (Street City State ZIP): Main Portland CO 12345


    Enter Data for Customer 6
    Enter First Last Phone: Doug Arizona 123-456-7890
    Enter Address (Street City State ZIP): Main Portland AZ 12345


    Enter Data for Customer 7
    Enter First Last Phone: Doug Florida 123-456-7890
    Enter Address (Street City State ZIP): Main Portland FL 12345


    Enter Data for Customer 8
    Enter First Last Phone: Doug Georgia 123-456-7890
    Enter Address (Street City State ZIP): Main Portland GA 12345


    Enter Data for Customer 9
    Enter First Last Phone: Doug Jones 123-456-7890
    Enter Address (Street City State ZIP): Main Portland CO 12345


    Enter 2-character state code: CO
    Data for Customer 4
    Account: 4
    Name: Doug Colorado
    Addr: Main Portland CO 12345
    Phone: 123-456-7890


    Data for Customer 5
    Account: 5
    Name: Another Colorado
    Addr: Main Portland CO 12345
    Phone: 123-456-7890


    Data for Customer 9
    Account: 9
    Name: Doug Jones
    Addr: Main Portland CO 12345
    Phone: 123-456-7890

    __________________________________________

    This is what I have so far:

    insert
    Code:
    #include <math.h>
    #include <stdlib.h>
    #include <stdio.h>
    #include <limits.h>
    #include <string.h>  
    #define SIZE 2 
    
    typedef struct {
        char firstName[30];
        char lastName[30];
        char street[35];
        char city[20];
        char state[3];
        int zip[8];
        char phone[15];
        int accountId;
    } Customer;
    
    int main()
    {
    
    Customer customer[10];
    int x;
    for (x = 0; x < 10; x++) {
    
              printf("Enter Data For Customer %d", x);
              printf("\n");
    
    
              printf("Enter First, Last, Phone: ");
              fgets(customer[x].firstName,10,stdin);  
              customer[x].accountId= strlen(customer[x].firstName) - 1;
              customer[x].firstName[customer[x].accountId] = '\0'; 
    
              fgets(customer[x].lastName,10,stdin); 
              customer[x].accountId= strlen(customer[x].lastName) - 1;
              customer[x].lastName[customer[x].accountId] = '\0';
    
              fgets(customer[x].phone,10,stdin); 
              customer[x].accountId= strlen(customer[x].phone) - 1;
              customer[x].phone[customer[x].accountId] = '\0';
    
              printf("Enter Address (Street, City, State, Zip): ");
    
              fgets(customer[x].street,10,stdin); 
              customer[x].accountId= strlen(customer[x].street) - 1;
              customer[x].street[customer[x].accountId] = '\0';  
    
              fgets(customer[x].city,10,stdin); 
              customer[x].accountId= strlen(customer[x].city) - 1;
              customer[x].city[customer[x].accountId] = '\0'; 
    
              fgets(customer[x].state,10,stdin); 
              customer[x].accountId= strlen(customer[x].state) - 1;
              customer[x].state[customer[x].accountId] = '\0'; 
    
              fgets(customer[x].zip,10,stdin); 
              customer[x].accountId= strlen(customer[x].zip) - 1;
              customer[x].zip[customer[x].accountId] = '\0';  
         }
    
    
         system("pause");
         return 0;
    
    }
    _______________

    I just cant figure out how to do the last question!! Please HELP ME!!! I need help with question 3!!!

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    23
    Quote Originally Posted by akemi.kanegae View Post
    I just cant figure out how to do the last question!! Please HELP ME!!! I need help with question 3!!!

    Pay attention with fgets:
    -Fgets automatically add the terminating null char after reading maximum N-1 chars.
    -Fgets returns a char*(or NULL) so you can't call it with an int variable (zip)
    -why do you specify 10 as number of maximum char to be read when firstName is an array of 30 char, state of 3 etc?
    Give a look here fgets - C++ Reference


    About the 3 point you can use strcmp - C++ Reference
    Last edited by root; 11-17-2012 at 02:18 AM.

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    16
    Hello,
    When you solve problems that root wrote, i would strongly recommend to use function in this program (you also get that hint in the assignment ). I would give you a idea how do it
    Code:
    Customer function (Customer x)
    {
        printf("Enter Name:")
        fgets(x.firstName,10,stdin); 
          // and so on //
         return x; // when you finished the function


  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Do you know the difference between C and C++?
    This is C, so it would benefit everyone if you would post questions regarding this language in the C section in the future.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed