Hi guys, I really need some of your help here because I don't know what to do now. Basically, this program will loop as many as the user inputs number of employee (numEmployee1). The program does the first loop fine, it stores and display the value whatever the user enters. After the first loop, the program does not work...it stores and displays some unexpected numbers (garbage numbers). I think there is something wrong with my loop but after 2 days I still cannot find the problem. Would you guys please help me here?
Here is a full version of my code:
Code:
#include <iostream>
#include <cstring>
#include <iomanip>
#include <cstdlib>
using namespace std;
//STRUCTURE, ENUM,AND UNION//////////////////////////////////////////////////////////////////////
enum payStatus{ Salaries1, Hourly1, Commissioned1};
union payRate
{
long salaries;
float hourly;
int commissioned;
};
struct Employee
{
char ID[4];
char name[36];
payStatus type;
payRate rate;
float grossPay;
};
////////////////////////////////////////////////////////////////////////////////////////////
//FUNCTIONS/////////////////////////////////////////////////////////////////////////////////
int getEmployeeData(Employee*, int);
float calculateGrossPay(Employee[], int);
void displayEmployeeInfo(Employee[], int, float);
////////////////////////////////////////////////////////////////////////////////////////////
//MAIN (STARTING POINT)/////////////////////////////////////////////////////////////////////
int main()
{
int numEmployee1;
Employee *emp;
float total;
cout<<fixed<<showpoint<<setprecision(2);
cout<<"Enter maximum number of employees to enter: ";
cin>>numEmployee1;
emp = new Employee[numEmployee1];
getEmployeeData(emp, numEmployee1);
total=calculateGrossPay(emp, numEmployee1);
displayEmployeeInfo(emp, numEmployee1, total);
delete [] emp;
return 0;
}
////////////////////////////////////////////////////////////////////////////////////////////
//ALL THE FUNCTION DEFINITIONS//////////////////////////////////////////////////////////////
int getEmployeeData(Employee* e, int numEmployee2)
{
char inString[80];
for(int i=0; i<numEmployee2; i++/*, e++*/)
{
cout<<"Enter employee id (Enter to quit): ";
cin.ignore();
cin.getline(e->ID,4);
cout<<"Enter name (Last, First): ";
cin.getline(e->name,36);
cout<<"Enter S or s for Salaried, H or h for Hourly, and C or c for Commissioned"<<endl;
cout<<"Enter payroll status: ";
cin.getline(inString, 80);
if(*inString=='S' || *inString=='s')
{
(e+i)->type=Salaries1;
cout<<"Enter monthly salary: ";
cin>>e->rate.salaries;
}
else if(*inString=='H' || *inString=='h')
{
(e+i)->type=Hourly1;
cout<<"Enter number of hours worked this month: ";
cin>>e->rate.hourly;
}
else
{
(e+i)->type=Commissioned1;
cout<<"Enter total sales for this month: ";
cin>>e->rate.commissioned;
}
//^^Error is at above here...it doesn't work from the second loop
}
return 0;
}
float calculateGrossPay(Employee e[], int numEmployee4)
{
float CalTotal=0.0;
for(int xx=0; xx<numEmployee4; xx++)
{
if(e[xx].type==Salaries1)
{
e[xx].grossPay = e[xx].rate.salaries;
}
else if(e[xx].type==Hourly1)
{
e[xx].grossPay = 18.75 * e[xx].rate.hourly;
}
else
{
e[xx].grossPay = 1000+(e[xx].rate.commissioned*0.06);
}
CalTotal+=e[xx].grossPay;
}
return CalTotal;
}
void displayEmployeeInfo(Employee e[], int numEmployee3, float finalTotal)
{
cout<<fixed<<showpoint<<setprecision(2);
cout<<endl;
cout<<endl;
cout<<"ID Name Payroll Status Gross Pay"<<endl;
cout<<endl;
for(int x=0; x<numEmployee3; x++)
{
cout<<left<<setw(9)<<e[x].ID<<setw(24)<<e[x].name;
if(e[x].type==Salaries1)
{
cout<<left<<setw(24)<<"Salaries"<<setw(5)<<e[x].grossPay<<endl;
}
else if(e[x].type==Hourly1)
{
cout<<left<<setw(24)<<"Hourly"<<setw(5)<<e[x].grossPay<<endl;
}
else
{
cout<<left<<setw(24)<<"Commissioned"<<setw(5)<<e[x].grossPay<<endl;
}
}
cout<<endl;
cout<<"Total Payroll for this month: $"<<finalTotal<<endl;
cout<<"Employee with Maximum gross pay: "<<endl;
cout<<"Employee with Minimum gross pay: "<<endl;
cout<<"Average gross pay: $"<<endl;
cout<<endl;
}
And here is the loop that I have problem with:
Code:
for(int i=0; i<numEmployee2; i++/*, e++*/)
{
cout<<"Enter employee id (Enter to quit): ";
cin.ignore();
cin.getline(e->ID,4);
cout<<"Enter name (Last, First): ";
cin.getline(e->name,36);
cout<<"Enter S or s for Salaried, H or h for Hourly, and C or c for Commissioned"<<endl;
cout<<"Enter payroll status: ";
cin.getline(inString, 80);
if(*inString=='S' || *inString=='s')
{
(e+i)->type=Salaries1;
cout<<"Enter monthly salary: ";
cin>>e->rate.salaries;
}
else if(*inString=='H' || *inString=='h')
{
(e+i)->type=Hourly1;
cout<<"Enter number of hours worked this month: ";
cin>>e->rate.hourly;
}
else
{
(e+i)->type=Commissioned1;
cout<<"Enter total sales for this month: ";
cin>>e->rate.commissioned;
}
//^^Error is at above here...it doesn't work from the second loop
}
Thank you very much for all of your inputs.
Vitamin_C