Passing a Nested Structure Array to a Function

Hello, friends. I'm pretty much a beginner here. I unfortunately had a very poor intro to c++ teacher that taught us only to pass the exam, not really stuff to implement during routine practice. I am now in Advanced c++ and I'm struggling pretty badly. I've been able to write the program completing all requirements except for one... creating a function that accepts a nested structure array. Here's the program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

struct PayInfo
{
    double hours;    // Hours worked
    double payRate;  // Hourly payRate
};

struct PayRoll
{
    int empNumber;    // Employee number
    string name;      // Employee's name
    double grossPay;  // Gross Pay
    PayInfo wage;     // nested structure within PayRoll

    PayRoll(int empN = 0, string n = " ", double gP = 0.00) // constructor
    {
       empNumber = empN;
       name = n;
       grossPay = gP;
    }
};

int main()
{
    const int numWorkers = 3;       // number of workers
    PayRoll employee[numWorkers];   // Array of structures
    int index;                      // Loop counter

    for(index = 0; index < numWorkers; ++index) {
        // Get the employee's number.
        cout << "Enter the employee number for employee number " << (index+1) << ": ";
        cin >> employee[index].empNumber;
        // Get the employee's name.
        cout << "Enter the name for employee number " << (index+1) << ": ";
        cin.ignore();  // To skip the '\n' character left in the input buffer
        getline(cin, employee[index].name);
        // Get the hours worked by the employee.
        cout << "How many hours did this employee work? ";
        cin >> employee[index].wage.hours;
        // Get the employee's hourly pay rate.
        cout << "What is the employee's hourly payRate? ";
        cin >> employee[index].wage.payRate;
        cout << endl;

        employee[index].grossPay = employee[index].wage.hours * employee[index].wage.payRate;  
// Calculate Gross -  THIS IS WHAT I NEED A FUNCTION FOR
    }
    cout << endl << "Here is the employee information:" << endl << endl;
    for(index = 0; index < numWorkers; ++index) {  // to print out array info
        cout << "Employee #: " << employee[index].empNumber << endl;
        cout << "Employee Name: " << employee[index].name << endl;
        cout << "Hours worked: " << employee[index].wage.hours << endl;
        cout << "Hourly pay Rate: " << employee[index].wage.payRate << endl;
        cout << fixed << showpoint << setprecision(2);
        cout << "Gross Pay: " << employee[index].grossPay << endl << endl;
    }
return 0;


I've been toiling over this for HOURS. I don't even know where to begin. Please help? Specifically, concerning all the aspects of the function.
Last edited on
Elaborate, what do you want to do and what that function should do. I do not see wny we might need function taking an array for in this code.
I honestly don't know why I'd need a function either. However, the assignment requirements say:

Write a function to calculate the grossPay member variable. Your function needs to accept a struct PayRoll variable. You may either pass it by reference OR pass the struct by value and RETURN the changed struct.

I hope that clarifies this a little better. Thanks for your response!
Ok, so function you need is:
1
2
3
4
5
6
void calculateGrossPay(PayRoll& employee)
{
    //calculate and set employee GrossPay
    //Any changes you make for this employee
    //will be reflected on original passed structure.
}
Thanks so much! Last question... Do I need a prototype for this? What would that even look like?
You need to forward declare function or make a prototype if you intend to use function befor it has been defined or if you want your function to be in another cpp file (in that case function prototype is usually placed into header)

Function prototype will look like void calculateGrossPay(PayRoll& employee);
Topic archived. No new replies allowed.