// 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;
}
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.