user puts in the number of employees but inputs less employees. He terminates the loop
by pressing the ENTER key. How do i do that. I tried strlen,, and even trid atoi but i dont know how to terminated
it. Also when it terminated its just uses that many employees that were entered and outputs the result.
The below method give me garbage. I need the return statment
to give me the count so i can display the results for the employees that were entered.
#include <iostream>
#include<sstream>
#include <string>
#include <iomanip>
using namespace std;
const float HOURLYPAYRATE=18.75;
const float COMMISSIONRATE=1000.00;
enum PayType {salary, hourly, commission};
union PayRate{
long salary;
float hours;
int commission;
};
struct Employee {
char id[4];
char name[80];
PayType type;
PayRate rate;
float grossPay;
};
int getEmployeeData(Employee* e, int size);
float calculateGrossPay(Employee [], int);
void displayEmployeeInfo(Employee [], int size);
int maxPayIndex(Employee[], int size);
int main()
{
int MAX=0;
cout<<"Enter maximum number of employees to enter?"<<endl;
cin>>MAX;
Employee* emp;
emp=new Employee[MAX];
if(emp==NULL)
{
cerr<<"couldn't allocate memory"<<endl;
return 1;
}
MAX=getEmployeeData(emp,MAX);
emp->grossPay=calculateGrossPay(emp,MAX);
displayEmployeeInfo(emp,MAX);
delete [] emp;
return 0;
}
int getEmployeeData(Employee* e, int size)
{
bool test=true;
int count=0;
for(int i=0; i<size && test ; i++)
{
count++;
stringbuf buffer;
cout << "Enter the employee's id (Enter to quit): ";
cin.get(buffer);
cout << buffer.str() << endl;
if ( buffer.str().empty() )
{
return count;
}
else
{
cout << "Enter name: ";
cin.ignore();
cin.getline((e+i)->name, 80);
cout << "Enter payroll status: ";
char input;
cin >> input;
cin.ignore();
if (input=='h'|| input=='H')
{
(e+i)->type=hourly;
cout<<"Enter number of hours worked this month: ";
cin>>(e+i)->rate.hours;
}
else if ( input== 's' || input =='S')
{
(e+i)->type=salary;
cout<<"Enter monthly salary: ";
cin>>(e+i)->rate.salary;
}
else if( input=='c'|| input=='C')
{
(e+i)->type=commission;
cout<<"Enter total sales this month: ";
cin>>(e+i)->rate.commission;
}
}
}
return count;
}
float calculateGrossPay(Employee e[], int size)
{
for(int i=0; i<size; i++)
{
if(e[i].type==salary)
{
e[i].grossPay= (float(e[i].rate.salary));
}
else if(e[i].type==hourly)
{
e[i].grossPay=(HOURLYPAYRATE * e[i].rate.hours);
}
else if(e[i].type==commission)
{
e[i].grossPay=float(COMMISSIONRATE + (0.06 * e[i].rate.commission));
}
}
return e->grossPay;
}
void displayEmployeeInfo(Employee e[], int size)
{
cout<<" ID Name PayrollStatus GrossPay\n";
cout<<endl;
for(int i=0; i<size; i++)
{
cout <<setw(4)<<e[i].id<<setw(10)<<" "<<e[i].name;
if(e[i].type==hourly)
{
cout<<setw(10)<<"Hourly";
cout << fixed << showpoint << setprecision(2);
cout<<setw(10)<< e[i].grossPay<< endl;
}
else if(e[i].type==salary)
{
cout<<setw(10)<<"Salaried";
cout<<fixed<<showpoint<<setprecision(2);
cout<<setw(10)<<e[i].grossPay<<endl;
}
else if(e[i].type==commission)
{
cout<<setw(10)<<"Commisioned";
cout<<fixed<<showpoint<<setprecision(2);
cout<<setw(10)<<e[i].grossPay<<endl;
}
}
int max =maxPayIndex(e,size);
cout<<"the employee with maxium gross pay is"<<e[max].name<<endl;
}
int maxPayIndex(Employee e[], int size)
{
float largest=e[0].grossPay;
int count=0;
for(int i=1; i<size; i++)
{
count++;
if(e[i].grossPay>largest)
largest=e[i].grossPay;
}
return count+1;
}