Error Shadow Parameter

// This program demonstrates the use of structures.
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdlib>

using namespace std;

const int NUM_EMPLOYEE = 3;

struct PayInfo
{
int hours; // Hours Worked
double payRate; // Hourly Pay Rate
};

struct PayRoll
{
PayRoll();
int empNumber; // Employee's number
string name; // Employee's name
double grossPay; // Gross Pay

PayInfo pay;
PayRoll (PayInfo& info, int num = 0, string Name = "", double gPay = 0.00)
{
empNumber = num;
name = Name;
grossPay = gPay;
pay.hours = info.hours;
pay.payRate = info.payRate;
}
};

void Gross(PayRoll& employee)
{
PayRoll employee[NUM_EMPLOYEE]; // employee is a PayRoll structure.];

// Calculate the employee's gross pay.
for(int i = 0; i < NUM_EMPLOYEE; i ++)
{
employee[NUM_EMPLOYEE].grossPay = employee[NUM_EMPLOYEE].pay.hours *
employee[NUM_EMPLOYEE].pay.payRate;
}
}
int main()
{
PayRoll employee[NUM_EMPLOYEE]; // employee is a PayRoll structure.

for(int i = 0; i < NUM_EMPLOYEE; i++)
{
// Get the employee's number.
cout << "Enter the employee's number: ";
cin >> employee[NUM_EMPLOYEE].empNumber;

// Get the employee's name.
cout << "Enter the employee's name: ";
cin.ignore(); // To skip the '\n' character left in the input buffer
getline(cin, employee[NUM_EMPLOYEE].name);

// Get the hours worked by the employee.
cout << "How many hours did the employee work? ";
cin >> employee[NUM_EMPLOYEE].pay.hours;

// Get the employee's hourly pay rate.
cout << "What is the employee's hourly payRate? ";
cin >> employee[NUM_EMPLOYEE].pay.payRate;
}

Gross(employee[NUM_EMPLOYEE]);

// Display the employee data.
cout << "Here is the employee's payroll data:\n";
cout << "name: " << employee[NUM_EMPLOYEE].name << endl;
cout << "Number: " << employee[NUM_EMPLOYEE].empNumber << endl;
cout << "hours worked: " << employee[NUM_EMPLOYEE].pay.hours << endl;
cout << "Hourly pay rate: " << employee[NUM_EMPLOYEE].pay.payRate << endl;
cout << fixed << showpoint << setprecision(2);
cout << "Gross Pay: $" << employee[NUM_EMPLOYEE].grossPay << endl;
return 0;
}

I get the error "Declaration of 'PayRoll employee [3]' shadows a parameter"
Please use code tags.
My guess, by quickly scanning your code, is that you have a problem in Gross function
1
2
3
void Gross(PayRoll& employee)
{
PayRoll employee[NUM_EMPLOYEE]; // employee is a PayRoll structure.]; 

You redefine employee
So how can I make it work?
Don't use the same name for your parameter as you use for a local variable. What exactly is its purpose in this case, anyway?
I think you don't need to declare the employee array inside your function
But if I don't declare it, the void function won't be able to know what "employee" is

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
63
64
65
66
#include <iostream>
#include <string>
#include <cstdlib>
#include <iomanip>

using namespace std;

struct PayInfo
{
    int hours;
    double payRate;
};

struct PayRoll
{
    PayRoll();
    int empNumber;
    string name;
    double grossPay;

    PayInfo pay;
};

void GrossPay()
{
    for (int i = 0; i < 3; i++)
    {
        employee[i].grossPay = employee[i].pay.hours * employee[i].pay.payRate;
    }
}

int main()
{
    PayRoll employee[3];

    for (int i = 0; i < 3; i++)
    {
        cout << "Enter the employee's number: " << endl;
        cin >> employee[i].empNumber;

        cout << "Enter the employee's name: " << endl;
        cin.ignore();
        getline(cin, employee[i].name);

        cout << "How many  hours did the employee work?" << endl;
        cin >> employee[i].pay.hours;

        cout << "What is the employee's hourly pay rate?" << endl;
        cin >> employee[i].pay.payRate;
    }

    GrossPay();

    for (int j = 0; j < 3; j++)
    {
        cout << "Here is the employee's payroll data:\n";
        cout << "name: " << employee[j].name << endl;
        cout << "Number: " << employee[j].empNumber << endl;
        cout << "hours worked: " << employee[j].pay.hours << endl;
        cout << "Hourly pay rate: " << employee[j].pay.payRate << endl;
        cout << fixed << showpoint << setprecision(2);
        cout << "Gross Pay: $" << employee[j].grossPay << endl;
    }

    return 0;
}


recently remade it.
Last edited on
You have tried two options; no declaration, which fails; and two declarations, which also fails. Try declaring it only once, as a formal parameter of the function while not also declaring it as a local variable.
Last edited on
How do I do that? I've never done that before >< been trying to looking it up but all I can think of is " PayRoll &employee "
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
63
64
65
66
67
#include <iostream>
#include <string>
#include <cstdlib>
#include <iomanip>

using namespace std;

struct PayInfo
{
    int hours;
    double payRate;
};

struct PayRoll
{
    PayRoll();
    int empNumber;
    string name;
    double grossPay;

    PayInfo pay;
};

void GrossPay(PayRoll employee[])
{
    for (int i = 0; i < 3; i++)
    {
        employee[i].grossPay = employee[i].pay.hours *
                                employee[i].pay.payRate;
    }
}

int main()
{
    PayRoll employee[3];

    for (int i = 0; i < 3; i++)
    {
        cout << "Enter the employee's number: " << endl;
        cin >> employee[i].empNumber;

        cout << "Enter the employee's name: " << endl;
        cin.ignore();
        getline(cin, employee[i].name);

        cout << "How many  hours did the employee work?" << endl;
        cin >> employee[i].pay.hours;

        cout << "What is the employee's hourly pay rate?" << endl;
        cin >> employee[i].pay.payRate;
    }

    GrossPay(PayRoll employee[]);         //Problem is Here

    for (int j = 0; j < 3; j++)
    {
        cout << "Here is the employee's payroll data:\n";
        cout << "name: " << employee[j].name << endl;
        cout << "Number: " << employee[j].empNumber << endl;
        cout << "hours worked: " << employee[j].pay.hours << endl;
        cout << "Hourly pay rate: " << employee[j].pay.payRate << endl;
        cout << fixed << showpoint << setprecision(2);
        cout << "Gross Pay: $" << employee[j].grossPay << endl;
    }

    return 0;
}


Error: expected primary-expression before 'employee'
GrossPay(employee);
Topic archived. No new replies allowed.