1

The errors I received are these:

error: 'NoOfHours' was not declared.

Its the same with taxrate, salary, tax, and netpay.

The output I'm looking for is a table using the gathered data from the user:

| Name     | Position       |   No. Of Hours |   Salary   |Tax|netpay|
| -------- | -------------- |----------------|------------|---|-|
| Mary     | Laborer        |    8           |500|25|475
| john     | Laborer        |    8           |500|25|475

Something like the table above.

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
    int Laborer = 500;
    int Foreman = 750;
    int Manager = 1500;
    int RatePerDay [3];

    int n;
    cout << "Enter No. of Employees: ";
     cin >>n;
    cout << endl;
    string Name [n];
    string Position [n];

    for ( int i=0; i<n; i++){

    float NoOfHours [i];
    float TaxRate [i];
    float Salary [i];
    float Tax [i];
    float NetPay [i];
    float TotalSalary[i];
    float TotalTax[i];
    float TotalNetPay[i];

    cout << "Enter Name: ";
     cin >> Name[i];

    cout << "Enter the position: ";
     cin >> Position[i];

    cout << "Enter No. of Hours Worked: ";
     cin >> NoOfHours[i];
    cout << endl;

    if (Position[i]=="Laborer")
        {RatePerDay[i] = Laborer;
        TaxRate[i] = 0.05;}

    else if (Position[i]=="Foreman")
        {RatePerDay[i] = Foreman;
        TaxRate[i] = 0.08;}

    else (Position[i]=="Manager");
        {RatePerDay[i] = Manager;
        TaxRate[i] = 0.10;}

    Salary[i] = NoOfHours[i] * RatePerDay[i] / 8;
    Tax[i] = Salary[i] * TaxRate[i];
    NetPay[i] = Salary[i] - Tax[i];

    TotalSalary[i] = TotalSalary[i] + Salary[i];
    TotalTax[i] = TotalTax[i] + Tax[i];
    TotalNetPay[i] = TotalNetPay[i] + NetPay[i];

    }
    cout <<setw(1)   <<right<< "Name of Employee"
         <<setw(15)  <<right<< "Position"
         <<setw(15)  <<right<< "No of Hours"
         <<setw(15)  <<right<< "RatePerDay"
         <<setw(15)  <<right<< "Salary"
         <<setw(15)  <<right<< "TaxRate"
         <<setw(15)  <<right<< "Tax"
         <<setw(15)  <<right<< "NetPay"<<endl;
    cout << "============================================================================================================================"<<endl;

    // I received the errors here.
    for ( int i=0; i<n; i++){

    cout << setw(23) <<setfill(' ')<<left<< Name[i]
         << setw(17) <<setfill(' ')<<left<< Position[i]
         << setw(14) <<setfill(' ')<<left<< NoOfHours[i]
         << setw(17) <<setfill(' ')<<left<< RatePerDay[i]
         << setw(14) <<setfill(' ')<<left<< Salary[i]
         << setw(18) <<setfill(' ')<<left<< TaxRate[i]
         << setw(12) <<setfill(' ')<<left<< Tax[i]
         << setw(15) <<setfill(' ')<<left<< NetPay[i]<<endl;
    }

    return 0;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
GrimAge
  • 23
  • 4
  • not valid c++ - array dimensions must be compile-time constants – Neil Butterworth Oct 18 '22 at 00:58
  • 1
    @NeilButterworth More like just *non-standard* C++. Some compilers DO allow variable-length arrays as an extension. But yes, they should be avoided. [Why aren't variable-length arrays part of the C++ standard?](https://stackoverflow.com/questions/1887097/) But that is not the root of the OP's problem. – Remy Lebeau Oct 18 '22 at 00:59
  • @Remy if it ain't standard, it ain't c++, but some other language – Neil Butterworth Oct 18 '22 at 01:00
  • 2
    Side note: Parallel arrays are a book-keeping nightmare. You're almost always better off with a structure that aggregates all of the data and then a single array of that structure. – user4581301 Oct 18 '22 at 01:01
  • Your `else`: `else (Position[i]=="Manager");` and the following lines definitely look funny. Is this really what you wanted? – Chris Oct 18 '22 at 01:11
  • *"The output I'm looking for is [...]"* -- functionality is irrelevant when your code won't compile. Focus on the compilation error. – JaMiT Oct 18 '22 at 01:29
  • A useful trick is arguing with your compiler. Point to the place in the code where the compiler says that `NoOfHours` is not declared, and point to the place where you believe it is declared. To make it easier to point, strip out as much of the code as you can while keeping these two code points. – JaMiT Oct 18 '22 at 01:30
  • @Ja a comment not an answer – Neil Butterworth Oct 18 '22 at 01:59

1 Answers1

2

Most of your arrays are declared inside of the 1st loop, so they are out of scope when the 2nd loop tries to access them. Fix your code's indentation, then the problem will be easier to see.

You need to move the affected arrays outside of the 1st loop, at the same level as the RatePerDay, Name, and Position arrays 1.

1: BTW, variable-length arrays are not standard C++. Use std::vector instead when you need an array whose sizes is not known until runtime.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770