Home > Net >  I don't know why my c arrays are not working
I don't know why my c arrays are not working

Time:10-18

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

CodePudding user response:

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.

  •  Tags:  
  • c
  • Related