Counter issues

Can someone explain to me what I am doing wrong in regards to the "EmployeeCounter" in the (Main) section of the code? It compiles and runs, but when you enter more than 1 employee it does not count up. It remains at employee 1. Here is the code:

#include <iostream>
using namespace std;

class payroll{
public: void setVariables(int empID, string fName, string lName, int stat, double rate, double hrs){
int employeeID;
string firstName;
string lastName;
int payStat;
double hourlyRate,salary,hours;
void printdata();

employeeID = empID; firstName = fName; lastName = lName; payStat = stat;
if (payStat == 1){ hourlyRate = rate; }
else { salary = rate;}
hours = hrs; }
//declare function to calculate gross pay
public: virtual double calculateGrossPay() = 0;
double taxRate,taxAmount,grossPay,netPay;
double calculateTaxAmount(){ taxRate = .30; //set a flat taxrate 30%
taxAmount = grossPay * taxRate; //formula to calculate tax amount
return taxAmount; } //end calculateTaxAmount() function
double calculateNetPay(){
return netPay; } //end
void printData(){ //print out the data
}//end printData() function
}; //end Payroll class

class employeeSalary : public payroll{
public: double calculateGrossPay() {
double regPay,hourlyRate,rate,hours,otHours,otPay;
if (hours > 40) {otHours = (hours - 40); //calculate OT hours
otPay = (otHours * hourlyRate); //calculate OT pay
grossPay = (regPay + otPay); }
else if (hours <= 40) {otHours = 0; otPay = 0; grossPay = regPay;}
return grossPay; }
}; //end EmployeeSalary class

class employeeHourly : public payroll{
public: double calculateGrossPay(){
double regPay,hourlyRate,otHours,hours,otPay;
regPay = (40 * hourlyRate); //calculate regular hours
if (hours > 40){ otHours = (hours - 40); //calculate OT hours
otPay = (otHours * hourlyRate * 1.5); //calculate OT pay
grossPay = (regPay + otPay); //calculate gross pay
} //enf if clause for gross pay with overtime
else { otHours = 0; otPay = 0; grossPay = regPay;
} //end else clause for four hours
return grossPay; } //end calculateGrossPay() function
}; //end EmployeeHourly class

void printHeader(){
}//end printHeader() function

int main(void){
int EmployeeCount,totalEmployeeCount,employeeCounter,stat,empID;
double hrs,rate,Grosspay,TaxAmount,NetPay;
string fName,lName;
cout<<"Enter # of employees you want to process: ";
payroll *employee[100];
//while loop to get input for each employee
while (employeeCounter < totalEmployeeCount){

//prompt the user for hourly or salary employee
cout<<"Is employee "<<employeeCounter+1<<" hourly or salary? (Enter 1 for HOURLY / 2 for SALARY): ";
if (stat == 1){cout<<"Instantiating an HOURLY employee object inherited from base class payroll..."<<endl<<endl;
cout<<"Enter employee's ID: ";cin>>empID;
cout<<"Enter employee's first name: ";cin>>fName;
cout<<"Enter employee's last name: ";cin>>lName;
cout<<"Enter employee's hourly wage: "; cin>>rate;
cout<<"Enter employee's hours for this week: "; cin>>hrs;
employee[employeeCounter] = new employeeHourly();
employee[employeeCounter]->setVariables(empID, fName, lName, stat, rate, hrs);
} //end if

cout<<"Instantiating a SALARY employee object inherited from base class payroll..."<<endl<<endl;


}//end while
}//end main
In your while loop, you're not incrementing the EmployeeCount variable. Be sure to start the value at 0 then near the end of the loop say EmployeeCount++.

Or do a for loop:

for(EmployeeCount = 0; EmployeeCount < TotalEmployeeCount; EmployeeCount++)

Edit: I mean to say your employeeCounter variable.
Last edited on
Topic archived. No new replies allowed.