Help working with functions, structures, and arrays

This is a discussion on Help working with functions, structures, and arrays within the C Programming forums, part of the General Programming Boards category; I'm having problems finishing this, I'm missing something but I can't figure it out. This is the goal and below ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    3

    Help working with functions, structures, and arrays

    I'm having problems finishing this, I'm missing something but I can't figure it out.

    This is the goal and below is the code I'm having problems with.

    Your program should create an array that contains ten (10) variables of the following type:

    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;

    You need to 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.

    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.
    Input

    1. Values for all component variables in each of the Customer variables.
    2. State code to match

    Output

    Values of the Customer variables that contain the desired state code
    Sample (user input in blue italics, 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



    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define SIZE 2 // initially to test kept it to SIZE 2
       
           
       
    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;
          
    Customer getCustInfo(int a )
    {
    Customer cust;
    char firstName2[30];
    printf(" Enter Data for Customer %d\n", a);
    printf("Enter First Last Phone: ");
    scanf("%s %s %s", cust.firstName, cust.lastName, cust.phone);
    printf("Enter Address (Street City State ZIP): \n");
    scanf("%s %s %s %d", cust.street, cust.city, cust.state, &cust.zip);
    cust.accountId = a + 1;
    return cust;
    } 
      
    void printCustDB(Customer cust) 
    {
    printf("Data for Customer %d\n", cust.accountId);
    printf(" First Last Phone: %s %s %s \n", cust.firstName, cust.lastName, cust.phone);
    printf(" Address (Street City State ZIP): %s %s %s %d\n", cust.street, cust.city, cust.state, cust.zip);
    }
     
    int main(void) 
    {
      
    Customer custDB[SIZE]; // an array of Customer type of size SIZE
    int i;
    char stateCode[3];
      
    for (i = 0; i < SIZE; ++i) 
    {
    custDB[i] = getCustInfo(i);
    }
          
    printf("Enter 2-character state code: ");
    scanf(" %c",&stateCode);
        
    for(i=0;i<SIZE;i++) 
    {if(custDB[i].state == stateCode[3]) 
    printCustDB(custDB[i]);
    }  
          
    system("pause");
    return (0);
      
    }
    It just lists all the entry's and I only want it to list the ones that i search for.
    Last edited by leway; 05-09-2010 at 05:21 PM.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    3
    printCustDB(custDB[i]);

    works fine.

    {if(custDB[i].state == stateCode)

    i printed out both (custDB[0].state and stateCode) and they have the same values so why is it not printing out printCustDB(custDB[i]);.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    There is a difference between == and strcmp.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    3
    Quote Originally Posted by tabstop View Post
    There is a difference between == and strcmp.
    Thanks a bunch, from this i got the code working.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with Nested Structures and Arrays of Structures
    By Ignoramus in forum C Programming
    Replies: 4
    Last Post: 03-02-2010, 01:24 AM
  2. Structures and dynamically allocated arrays
    By Bandizzle in forum C Programming
    Replies: 7
    Last Post: 10-04-2009, 03:05 PM
  3. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 10:04 AM
  4. Working with arrays of structures, need advice...
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 08-14-2002, 09:38 AM
  5. errors with arrays and structures
    By ssjnamek in forum C++ Programming
    Replies: 4
    Last Post: 03-03-2002, 11:48 PM

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