Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

CPSCI 121-Spring 2017 Program #5, due Thurs 3/9/17 by 11pm on Titanium Revisitin

ID: 3827485 • 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>
using namespace std;

struct Employee{
   string name; //can have spaces
   int years; //years of service
   double salary;
};

int main(){

   struct Employee eArray[5];
   int totalDone = 0;
   for(int i = 0; i < 5; i++){
       cout << "Enter employee name: ";
       getline( cin, eArray[i].name );
       //YEARS OF SERVICE
       cout << "Enter years of service: ";
       cin >> eArray[i].years;
       while( eArray[i].years <= 0 ){
           cout << " Sorry, value must be positive. Try again." << endl;
           cout << "Enter years of service: ";
           cin >> eArray[i].years;
       }
       //SALARY
       cout << "Enter salary: ";
       cin >> eArray[i].salary;
       while( eArray[i].salary <= 0 ){
           cout << " Sorry, value must be positive. Try again." << endl;
           cout << "Enter salary: ";
           cin >> eArray[i].salary;
       }

       //got employee information
       char prompt;
       cout << "Enter another employee (Y/N)? ";
       cin >> prompt;
       if( !( prompt <= 'Z' && prompt >= 'A' )){
           prompt = prompt + 'A' - 'a' ; //capitalize this character
       }
       while( prompt != 'Y' && prompt != 'N' ){
           cout << "*" << prompt << "*" << endl;
           cout << " Sorry, answer must be Y or N. Try again." << endl;
           cout << "Enter another employee (Y/N)? ";
           cin >> prompt;
           if( !( prompt <= 'Z' && prompt >= 'A' )){
               prompt = prompt + 'A' - 'a' ; //capitalize this character
           }
       }
       string tpass;
       getline( cin, tpass);
       totalDone = totalDone + 1;
       if( prompt == 'N' ){ break; }
   }

   double averageYears = 0.0;
   double averageSalary = 0.0;
   for(int i = 0; i < totalDone; i++){  
       averageSalary = averageSalary + eArray[i].salary;
       averageYears = averageYears + eArray[i].years;
   }
   averageYears = averageYears/totalDone;
   averageSalary = averageSalary/totalDone;

   cout << "Name Years Salary" << endl;
   for(int i = 0; i < totalDone; i++){
       cout << eArray[i].name << " " << eArray[i].years << " " << eArray[i].salary << endl;
   }
   cout << endl;
   cout << "Average " << averageYears << " " << averageSalary << endl;

   return 0;
}