-3
#include<iostream>
#include<string>
#include<iomanip>

using namespace std;

struct EmployeeData
{
    string employeeName;
    float overtime;
    float grossPay;
    float hoursWorked;
    float hourlyRate;
    float statetaxOwed;
    float statetaxRate;
    float fedtaxOwed;
    float fedtaxRate;
    float netPay;
};

EmployeeData employee[4]; //array of 4 employees


int main()
{
for (int i = 0; i < 4; ++i)
    {
    cout << "Please enter the Employee's Name: ";
    cin >> employee[i].employeeName;
    cout << "Please enter your hours worked: ";
    cin >> employee[i].hoursWorked;
    cout << "Please enter your hourly rate: ";
    cin >> employee[i].hourlyRate;
    cout << "Please enter the Federal Tax Rate: ";
    cin >> employee[i].fedtaxRate;
    cout << "Please enter the State Tax Rate: ";
    cin >> employee[i].statetaxRate;
    }

for (int i = 0; i < 4; ++i)
    {
    calculate_stats(employee[i]);
    }

}

void calculate_stats(EmployeeData& employee)
{
    if (employee[i].hoursWorked>40) {
        employee[i].hoursWorked = ((employee[i].hoursWorked-40) * (1.5)) + 40;
    }
    else {
        employee[i].hoursWorked = employee[i].hoursWorked;
    }
    employee[i].grossPay = employee[i].hoursWorked * employee[i].hourlyRate;
    employee[i].fedtaxOwed = employee[i].grossPay * (employee[i].fedtaxRate/100);
    employee[i].statetaxOwed = employee[i].grossPay * (employee[i].statetaxRate/100);
    employee[i].netPay = (employee[i].grossPay - employee[i].fedtaxOwed- employee[i].statetaxOwed);

    cout << setprecision(2) << showpoint << fixed;
    cout << "\nThe employee's name is: " << employee[i].employeeName << endl;
    cout << "The Gross Pay is: $" << employee[i].grossPay << endl;
    cout << "The Federal Taxes Owed is: $" << employee[i].fedtaxOwed << endl;
    cout << "The State Taxes Owed is: $" << employee[i].statetaxOwed << endl;
    cout << "The Net Pay for the Employee is: $" << employee[i].netPay << endl;
}

On line if employee[i].hoursWorked > 40) I recieve the error: "ERROR: IDENTIFIER "i" IS UNDEFINED. Yet I made sure to define it in the main. Is my code organized properly? If it's not, how should I organize it? I have a feeling this has to do with how my code is organized.

Elsa
  • 1
  • 1
  • 1
  • 1
    i in other places are local, can't accessed in other functions. – Shiping Jan 31 '17 at 02:36
  • Variables are only visible within the scope they are defined. `i` is not visible outside of `main`. In fact, it is not visible outside of that `for` loop in which it is defined. I recommend getting a good book on C++: you can find some recommendations [here](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Greg Kikola Jan 31 '17 at 02:36
  • Your `calculate_stats` function takes _one_ employee, not an array... remove the unnecessary (and incorrect) `[i]` from that function – Chad Jan 31 '17 at 02:37
  • Thank you Chad. I will try removing the `[i]` from the void variables. – Elsa Jan 31 '17 at 02:39
  • I have a new error, however...when I solved that problem it gave me a new error (only occurs when I try to run, it doesn't show up otherwise)...it says ERROR C3861: 'calculate_stats' : identifier not found. It is referencing the calculate_stats in the for-loop. I was hoping to use that to call the void and do the number crunching and printing. – Elsa Jan 31 '17 at 02:48
  • Did you declare `calculate_stats` before `main`? You have to have a declaration before you can use a function. – Greg Kikola Jan 31 '17 at 02:56
  • this worked: `void calculate_stats(EmployeeData& employee);` thank you. I had to declare it before the main alongide `employeeData employee[4]` – Elsa Jan 31 '17 at 03:16

1 Answers1

0

i you defined is local and can't be accessed in other functions. You may change those codes like the following.

void calculate_stats(EmployeeData&); // put this line before main function.

... 
for (int i = 0; i < 4; ++i)
{
   calculate_stats(employee[i]);
}
...


void calculate_stats(EmployeeData& employee)
{
    if (employee.hoursWorked>40) {
        employee.hoursWorked = ((employee.hoursWorked-40) * (1.5)) + 40;
    }
    else {
        employee.hoursWorked = employee.hoursWorked;
    }
    ...
Shiping
  • 1,203
  • 2
  • 11
  • 21
  • Thank you for helping. It means a lot. I have a new error, however...when I solved that problem it gave me a new error (only occurs when I try to run, it doesn't show up otherwise)...it says ERROR C3861: '`calculate_stats`' : identifier not found. It is referencing the calculate_stats in the for-loop. I was hoping to use that to call the void and do the number crunching and printing. – Elsa Jan 31 '17 at 02:48
  • @Elsa i made some changes in my post and i tested it and it worked. Forgot to mention that you need to add this line "void calculate_stats(EmployeeData *);" before the main function. – Shiping Jan 31 '17 at 03:38
  • @Elsa sorry you can just keep your previous version and change this line calculate_stats(employee + i); to calculate_stats(employee[i]i); and add this line "void calculate_stats(EmployeeData&);" before the main function. – Shiping Jan 31 '17 at 03:46