Code:
#include <iostream>
#include <cstring>
#include <iomanip>
#include <cstdlib>
#include <conio.h>
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, int&);
float calculateGrossPay(Employee[], int);
int maxPayIndex(Employee[], int);
int minPayIndex(Employee[], int);
float avgPay(int, float);
void displayEmployeeInfo(Employee[], int, float, float, int, int);
bool isValidID(Employee[], int);
//bool tryAgain
//bool isValidStatus
////////////////////////////////////////////////////////////////////////////////////////////
//MAIN (STARTING POINT)/////////////////////////////////////////////////////////////////////
int main()
{
int numEmployee, max, min, count=0;
Employee *emp;
float total, average;
cout<<fixed<<showpoint<<setprecision(2);
cout<<"Enter maximum number of employees to enter: ";
cin>>numEmployee;
emp = new Employee[numEmployee];
getEmployeeData(emp, numEmployee, count);
total=calculateGrossPay(emp, count);
max=maxPayIndex(emp, count);
min=minPayIndex(emp, count);
average=avgPay(count, total);
displayEmployeeInfo(emp, count, total, average, max, min);
delete [] emp;
return 0;
}
////////////////////////////////////////////////////////////////////////////////////////////
//ALL THE FUNCTION DEFINITIONS//////////////////////////////////////////////////////////////
int getEmployeeData(Employee* e, int numEmployee, int& count)
{
char inString[80][80];
int num;
for(int i=0; i<numEmployee; i++)
{
cout<<"Enter employee id (Enter to quit): ";
cin.ignore();
cin.getline(e[i].ID, sizeof(e[i].ID));
num=atoi(e[i].ID);
if(isValidID(e[i].ID, count)==true)//HERE IS THE PART THAT I AM HAVING PROBLEM WITH.
{
cout<<"Enter name (Last, First): ";
cin.getline(e[i].name, sizeof(e[i].name));
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[i], 80);
if(*inString[i]=='S' || *inString[i]=='s')
{
e[i].type=Salaries1;
cout<<"Enter monthly salary: ";
cin>>e[i].rate.salaries;
}
else if(*inString[i]=='H' || *inString[i]=='h')
{
e[i].type=Hourly1;
cout<<"Enter number of hours worked this month: ";
cin>>e[i].rate.hourly;
}
else
{
e[i].type=Commissioned1;
cout<<"Enter total sales for this month: ";
cin>>e[i].rate.commissioned;
}
count=count+1;
}
else
{
if(num==0)
{
return 0;
}
else
{
cout<<"*** Invalid employee ID ***"<<endl;
return 0;
}
}
}
return count;
}
float calculateGrossPay(Employee e[], int count)
{
float CalTotal=0.0;
for(int i=0; i<count; i++)
{
if(e[i].type==Salaries1)
{
e[i].grossPay = e[i].rate.salaries;
}
else if(e[i].type==Hourly1)
{
e[i].grossPay = 18.75 * e[i].rate.hourly;
}
else
{
e[i].grossPay = 1000+(e[i].rate.commissioned*0.06);
}
CalTotal+=e[i].grossPay;
}
return CalTotal;
}
float avgPay(int count, float total)
{
float av;
av=total/count;
return av;
}
int maxPayIndex(Employee e[], int count)
{
int bigger=0, x=0;
for(int i=0;i<count; i++)
{
if(e[i].grossPay>bigger)
{
bigger=e[i].grossPay;
x=i;
}
else
{
bigger=bigger;
}
}
return x;
}
int minPayIndex(Employee e[], int count)
{
int smaller=0, x=0;
smaller=e[0].grossPay;
for(int i=0;i<count; i++)
{
if(e[i].grossPay<smaller)
{
smaller=e[i].grossPay;
x=i;
}
else
{
smaller=smaller;
}
}
return x;
}
bool isValidID(Employee e[], int count)
{
int length;
length=strlen(e[count].ID);
if(length==3)
{
//for(int i=0; i<3; i++)
//{
//if(isalnum(e[i].ID))
//return true;
//else
//return false;
//}
return true;
}
else
return false;
}
void displayEmployeeInfo(Employee e[], int count, float finalTotal, float average, int max, int min)
{
int num;
num=atoi(e[0].ID);
if(num==0)
{
cout<<endl;
cout<<"***The program is terminated***"<<endl;
cout<<endl;
return;
}
else
{
cout<<fixed<<showpoint<<setprecision(2);
cout<<endl;
cout<<endl;
cout<<"ID Name Payroll Status Gross Pay"<<endl;
cout<<endl;
for(int i=0; i<count; i++)
{
cout<<left<<setw(9)<<e[i].ID<<setw(24)<<e[i].name;
if(e[i].type==Salaries1)
{
cout<<left<<setw(24)<<"Salaries"<<setw(5)<<e[i].grossPay<<endl;
}
else if(e[i].type==Hourly1)
{
cout<<left<<setw(24)<<"Hourly"<<setw(5)<<e[i].grossPay<<endl;
}
else
{
cout<<left<<setw(24)<<"Commissioned"<<setw(5)<<e[i].grossPay<<endl;
}
}
cout<<endl;
cout<<"Total Payroll for this month: $"<<finalTotal<<endl;
cout<<"Employee with Maximum gross pay: "<<e[max].name<<endl;
cout<<"Employee with Minimum gross pay: "<<e[min].name<<endl;
cout<<"Average gross pay: $"<<average<<endl;
cout<<endl;
}
}
Thank you very much C++ people