CPSCI 121-Spring 2017 Program #5, due Thurs 3/9/17 by 11pm on Titanium Revisitin
ID: 3839271 • Letter: C
Question
CPSCI 121-Spring 2017 Program #5, due Thurs 3/9/17 by 11pm on Titanium Revisiting program #1 using an array of structs. Create a struct type that will represent a single Employee, where each Employee has a name (a string that may contain blank spaces), years of service (a positive integer), and a salary (a positive double). Create an array of 5 Employees Make a loop that will go through a maximum of 5 times, but will ask the user ifthey want to go on with the loop after each time through. The loop ends either when it has executed the full 5 times or when the user chose to stop entering data. Inside the loop, do the following tasks: Get data for one Employee and store it into one element of the Employee array. Keep track of the total number of years and the total salary Ask the user if they want to enter another After the loop: Write out column headings Write out the name, years, and salary for each element of the Employee array. Calculate the averages Write out the average years and salary Use functions to do the major tasks. Required functions: Function to get data for one Employee and return that one Employee struct. Function to write the output for the entire array of Employees (all that have data, don't write anything for the ones that weren't used). Other functions may be used as well. The output should be formatted in a similar way to the output from program #1. (see sample run Comments Name block Function comments-descriptive comment before each definition Input must include error checking on both the years and salaryvaluesto ensure that they are positive values. The repeat option must also include an error check loop to make sure the user's response is a correct one (i.e. Y/N)Explanation / Answer
#include<iostream>
//#include<conio>
#include<iomanip>
using namespace std;
struct Employee{
char name[50];
float yrs;
float sal;
} ;
void display(Employee e[], int n, float yAvg, float sAvg){
cout << "Name" << std::setw(50);
cout<< std::setw(50) << "Years";
cout<< std::setw(11) << "Salary";
cout<<endl;
cout<< std::fixed;
for(int i=0;i < n; i++){
cout<< e[i].name << std::setw(50) ;
cout<< std::setw(53) << setprecision(0) << e[i].yrs;
cout<< std::setw(11) << setprecision(2) << e[i].sal;
cout<< endl;
}
cout << "Average";
cout<< std::setw(47) << setprecision(1) << yAvg;
cout<< std::setw(11) << setprecision(2) << sAvg;
cout<< endl;
}
int main(){
Employee emp[5];
char ch='Y';
int i = 0;
float sal,yrs,salAvg=0,yrAvg=0;
while(ch=='Y' || ch=='y'){
cout<<"Enter employee name: ";
cin>>emp[i].name;
emp[i].yrs = -1;
while(emp[i].yrs < 0){
cout<<"Enter years of service: ";
cin>>emp[i].yrs;
if(emp[i].yrs < 0)
cout<<"Sorry, value must be positive. Try again. ";
}
emp[i].sal = -1;
while(emp[i].sal < 0){
cout<<"Enter salary: ";
cin>>emp[i].sal;
if(emp[i].sal < 0)
cout<<"Sorry, value must be positive. Try again. ";
}
ch = 'x';
while(ch!='Y' && ch!='N'){
cout<<"Enter another employee (Y/N)? ";
cin>>ch;
if(ch!='Y' && ch!='N'){
cout<<"Sorry, answer must be Y or N. Try again. ";
}
}
salAvg+=emp[i].sal;
yrAvg+=emp[i].yrs;
i++;
if(ch=='N') break;
}
salAvg = (float)salAvg/(float)i;
yrAvg = (float)yrAvg/(float)i;
display(emp,i,yrAvg,salAvg);
return 0;
}