Help with last part of struct

This is a discussion on Help with last part of struct within the C Programming forums, part of the General Programming Boards category; Hi I have almost all of my program complete I think, but what I can't figure out is how do ...

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

    Angry Help with last part of struct

    Hi I have almost all of my program complete I think, but what I can't figure out is how do I include the Total Gross of all the employees at the end of my program. I know I need a printf statement but I'm not sure what else I need and how to make it work. Please help
    Code:
    #include <stdio.h>
    
    struct Info
    {
    int Id_Number;
    char Last_Name[20];
    float Pay_Rate;
    float Hours_Worked;
    float Gross_Pay;
    float Total_Gross_Pay;
    };
    int main()
    {
    struct Info info[1];
    
    int p;
    
    
    
        for (p = 0; p < 6; p++)
    {
            printf("\n\nEnter the employee Id Number? ");
            scanf("%d", &info[p].Id_Number);
            printf("\nEnter the employee Last Name? ");
            scanf("%s", &info[p].Last_Name);
            printf("\nEnter the employee Pay Rate? ");
            scanf("%f", &info[p].Pay_Rate);
            printf("\nEnter the employee Hours Worked? ");
            scanf("%f", &info[p].Hours_Worked);
                    
        info[p].Gross_Pay = ((info[p].Pay_Rate) * (info[p].Hours_Worked));
        
        
    }
        printf("\nLast Name          Id Number                  Gross Pay");
        printf("\n_________           _________                 _________");
        
        for (p = 0; p < 6; p++)
        {
        printf("\n%-20s %-20d      %2.2f", info[p].Last_Name, info[p].Id_Number, info[p].Gross_Pay);
    }
     
        printf("\n___________________________________________________________");
        printf("\n-----------------------------------------------------------");
        
        
    getchar();
    getchar();
     return 0;
      
        }

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Your array has only one element yet you are looping six times.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Since you left out details in your question, I fill them in myself.

    I assume that the "Info" struct represents one person. Therefore, it does not make sense to store the total of anything (i.e. total gross pay) for a bunch of "Infos" in the struct itself. The "total gross pay" should be something external to the struct, so just another variable in your "main". To get the total gross pay, you iterate over all "Infos" and sum their "gross pay" fields, and print it. I.e. the following pseudocode
    Code:
    sum = 0; // initialize sum
    for counter = 1 to size of array // if you only have one element, no need for an array, i assume you have a larger array
      // sum all the gross pays 
    print sum

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    16
    This is a single structure type.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    What is that? If you have an array with one element and the size of the array doesnt change, theres no point having an array.

  6. #6
    Registered User
    Join Date
    Oct 2009
    Posts
    16
    I'm sorry I wasn't thinking part of the question reads Declare a single structure type suitable for an employee record consisting of an array of six stuctures.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Thats a terribly worded sentence/assignment specification, in my opinion. What is a "single structure type"? Is it asking to create an array of 6 "Employee" structs?

  8. #8
    Registered User
    Join Date
    Oct 2009
    Posts
    16
    Ok I will type the entire question.
    A) Declare a single structure type suitable for an employee record consisting of an interger identification number, a last name(consisting of a moximum of 20 characters), a floating point pay rate and a floating point number of hours worked.

    B)Using the sturcture type declared in programming exercise A write a program that interactively accepts the following data into an array of six sturctures.

    C)Once the data has been entered the program shouyld create a payroll report listing each employee's name, number, and gross pay. Include the total gross pay of all employees at the end of the report.

  9. #9
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Hopefully youve learnt not to leave out details (as I mentioned above), because it only delays your answer.

    A) Declare a single structure type suitable for an employee record consisting of an interger identification number, a last name(consisting of a moximum of 20 characters), a floating point pay rate and a floating point number of hours worked.
    Code:
    struct Info
    {
    int Id_Number;
    char Last_Name[20];
    float Pay_Rate;
    float Hours_Worked;
    float Gross_Pay;
    float Total_Gross_Pay;
    };
    Youve declared more fields in the record that it asks for. It asks for 4 fields but youve created 6. Also, the more intuitive struct name might be "Employee", as the assignment suggests, rather than "Info".

    B)Using the sturcture type declared in programming exercise A write a program that interactively accepts the following data into an array of six sturctures.
    You seem to have this part done, however, as mentioned twice above, you need an array of 6 structures--right now you have an array of 1 structure.

    C)Once the data has been entered the program shouyld create a payroll report listing each employee's name, number, and gross pay. Include the total gross pay of all employees at the end of the report.
    You seem to have most of this done. However, you have to calculate each employees pay in this second for loop (rather than storing it in the struct, as you do now in the first for loop--you can only use the 4 given fields and cannot add others like this one). For displaying the total gross pay of all employees see my first reply.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with structs and malloc!
    By coni in forum C Programming
    Replies: 20
    Last Post: 09-14-2009, 05:38 PM
  2. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  3. Replies: 1
    Last Post: 12-03-2008, 02:10 AM
  4. Replies: 16
    Last Post: 10-29-2006, 04:04 AM
  5. Search Engine - Binary Search Tree
    By Gecko2099 in forum C Programming
    Replies: 9
    Last Post: 04-17-2005, 02:56 PM

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