Help me with a program for my class!

This is a discussion on Help me with a program for my class! within the C Programming forums, part of the General Programming Boards category; Hello, I am new here. I just stared a C programming class and realized that it is wayyyy over my ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    10

    Help me with a program for my class!

    Hello, I am new here. I just stared a C programming class and realized that it is wayyyy over my head. But I want to at least get a decent grade. I have to make this program and I am completely lost on how to do it. Can someone help me?


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

    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;
    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

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    35
    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;
    what you want to do is write a function for each of the elements in the struct. the function will take in a pointer to a struct and a string/integer and place it in the struct.

    so your first function will take in a Customer and a char* and what it will do is put the char* into the Customer struct

    i dont quite get the next part. what is a state string? which string inside the struct are we using to compare the state string to?

    but I think you can get started now on writing the functions

    (hint: you might like to create another function that will take in a struct and set all of its arguments to null. then you can create another function, printStruct, which prints out what is in the struct. you can then call printStruct at the start and end of every function. this can help you ensure that your functions are doing what you intend for them to do.)

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    10
    I don't think I know how to do that

  4. #4
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    It isn't really essential to write a function to fill in each element, even though it may make things a little bit easier.

    A fairly easy way to do it, would be to declare the array of ten structures, then create a loop which loops through ten times, and assigns the input values into the structures.

    If you aren't familiar with pointers or arrays, then you will want to read up on them, because you will have to be able to seperate the parts of the address.

    To compare them you can have another loop and use strcmp() on the structures to see if the states match, then print them if they do.

    EDIT: Actually, now that I think of it, you could use sscanf() to remove the different parts of the address and put them into the different members of the structure.

  5. #5
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    PusdoCode for this

    [quote]
    input 1 to 9 into the structure
    input first element put into the structure
    and so on
    [quote]

    then call a function compare kind of

    if input key is matched with the structure element display the whole structure

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class design problem
    By h3ro in forum C++ Programming
    Replies: 10
    Last Post: 12-19-2008, 08:10 AM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 04:01 PM
  3. Replies: 8
    Last Post: 10-02-2005, 12:27 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. C program for Class --- DUE TODAY!!! AHHHH
    By Fr0st2k in forum C Programming
    Replies: 6
    Last Post: 09-22-2004, 03:10 PM

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