#include<iostream.h>
#include<stdlib.h>
#include<string.h>
#define BUFSIZE 20
class worker{
char *name;
double hoursworked,hourpay,overpay,bsalary,tax,nsalary;
char status;
public:
worker():hoursworked(0.0),hourpay(0.0),overpay(0.0 ),status('s'),
bsalary(0.0),tax(0.0),nsalary(0.0)
{}
void readdata(void);
void calculate(void);
char* getname(void){return name;}
void showdata(void);
};
void worker::readdata(void) /* Function for input of a workers data */
{
char buffer[BUFSIZE];
cout << "WORKER'S NAME: ";
cin.getline(buffer,BUFSIZE); // input: worker's name
if(!buffer[0])
buffer[0]='?',buffer[1]='\0';
name=new char[strlen(buffer)+1];
if(name!=NULL)
strcpy(name,buffer);
else exit(1);
do{ // input: hours worked, make sure you get a value
cout << "HOURS WORKED: ";
cin.getline(buffer,BUFSIZE);
if(buffer[0]=='0')
{hoursworked=0.0; break;}
hoursworked=atof(buffer); /* atof returns zero, if a string cant be converted
to a 'double' value */
}while(!hoursworked);
do{ // input: fee/hour
cout << "FEE / HOUR: ";
cin.getline(buffer,BUFSIZE);
if(buffer[0]=='0')
{hourpay=0.0; break;}
hourpay=atof(buffer);
}while(!hourpay);
do{ // input: fee/overtime
cout << "FEE / OVERTIME: ";
cin.getline(buffer,BUFSIZE);
if(buffer[0]=='0')
{overpay=0.0; break;}
overpay=atof(buffer);
}while(!overpay);
do{ // input: fee/hour
cout << "STATUS (marriaged, single m/s): ";
cin.getline(buffer,BUFSIZE);
status=buffer[0];
}while(status!='s' && status!='m');
} // End of readdata
void worker::calculate(void)
{
hoursworked<=40 ? (bsalary=hoursworked*hourpay) :
(bsalary=(hoursworked-40)*overpay+40*hourpay);
status=='s' ? (tax=bsalary*0.3) : (tax=bsalary*0.25);
nsalary=bsalary-tax;
}
void worker::showdata(void)
{
cout << "NAME: " << name << '\n' << "STATUS: " << status
<< '\n' << "SALARY: " << nsalary << '/' << bsalary
<< " (TAX: " << tax << ')' << '\n' << '\n';
}
void main()
{
char buffer[BUFSIZE];
int howmany=0;
do{
cout << "How many workers ? >> ";
cin.getline(buffer,BUFSIZE);
howmany=atoi(buffer);
}while(!howmany);
howmany=(howmany<0 ? -howmany : howmany);
worker *wkptr=new worker[howmany];
if(wkptr==NULL) // HEAPOVERFLOW, NULL is def in stdlib
{cout << "HEAP !!!"; exit(1);}
for(int i=0;i<howmany;i++){
wkptr[i].readdata();
wkptr[i].calculate();
cout.put('\n');
} // End of for
cout.put('\n');
do{
cout << "Input name of a worker\nto see his data: ";
cin.getline(buffer,BUFSIZE);
if(buffer[0]=='\0')
{buffer[0]='?'; buffer[1]='\0';}
for(i=0;i<howmany;i++){
if(!(strcmp(buffer,wkptr[i].getname())))
{wkptr[i].showdata();break;}}
if(i==howmany)
cout << "NOT FOUND\n";
cout << "ANY KEY OR e FOR END >> ";
cin.getline(buffer,BUFSIZE);
if(buffer[0]=='e')
break;
}while(1);
} // End of main, delete is done automatically