Sorry I would split this into multiple questions but each part depends on the pr
ID: 3850874 • Letter: S
Question
Sorry I would split this into multiple questions but each part depends on the previous.
Part 1:
Exercise 1.Modify the below program so that instead of initializing employee information in the code (with the initializations), it prompts the user to input the data (name, id, and pay rate) from the standard input device (keyboard) and stores it in variables employee1 and employee2.
// This program demonstrates the use of structure variables.
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
struct EmployeeRec
{
string name; // Employee name
String empId; // Employee number
double payRate; // Hourly pay rate
double hours; // Hours worked
};
int main()
{
EmployeeRec employee1 = {"Betty Ross", 14001, 18.75};
EmployeeRec employee2 = {"Rashid Wallace", 14002, 17.50};
double grossPay;
cout << fixed << showpoint << setprecision(2);
// Calculate pay for employee1
cout << "Name: " << employee1.name << endl;
cout << "Employee Number: " << employee1.empId << endl;
cout << "Enter the hours worked by this employee: ";
cin >> employee1.hours;
grossPay = employee1.hours * employee1.payRate;
cout << "Gross Pay: " << grossPay << endl << endl;
// Calculate pay for employee2
cout << "Name: " << employee2.name << endl;
cout << "Employee Number: " << employee2.empId << endl;
cout << "Enter the hours worked by this employee: ";
cin >> employee2.hours;
grossPay = employee2.hours * employee2.payRate;
cout << "Gross Pay: " << grossPay << endl;
return 0;
}
Part 2: Defining functions to manipulate structures
Exercise 1.Write a function called getEmpployeeInfo() that prompts the user to enter an employee information (name, id, and pay rate) from standard input and returns it as a EmployeeRec struct. Call the function to get information for employee1 and employee2.
Exercise 2.Write a function called displayEmpployeeInfo() that takes and EmployeeRec as a parameter and displays the value of its data members on the console using the following format:
Name: -----------------------------------
ID: ----------------------------------
Pay Rate: ----------------------------------
Hours Worked: ----------------------------------
Gross pay: ----------------------------------
Exercise 3.Modify the above program to use function displayEmployeeInfo() to output the employees information.
Part 3: Arrays of Structures
Exercise 1.Instead of using separate variables to store employee information (employee1, employee2, etc.), modify you program from the previous task to declare an array of structs to hold the information of up to 100 employees. Name the array empList
Exercise 2.Use function getEmployeeInfo() to read data for at least 3 employees into array empList. You can prompt the user to tell you how many employees they want to enter. Be sure to save the number of employee entered into a variable.
Exercise 3.Use function displayEmplyeeInfo() to display the employee information in empList. Use the same output format at in the previous task.
Part 4: Hierarchical Structures
Exercise 1.Add another member to the EmployeeInfo struct to store the employee’s address. The address member should be a struct data type with the following members:
Street
City
State
Zip code
Exercise 2.Modify your program of Task 3, including the get and display functions, to incorporate this change.
Explanation / Answer
The answer is as follows:
The modifications are as follows:
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
struct address {
string Street;
string City
string State;
int Zip_code;
};
struct EmployeeRec
{
string name; // Employee name
String empId; // Employee number
double payRate; // Hourly pay rate
double hours; // Hours worked
struct address emp_addr; // Employees Address
};
struct EmployeeRec getEmployeeInfo(){
struct EmployeeRec emp;
cout << "Name: ";
cin >> emp.name;
cout << "Employee Number: ";
cin >> emp.empId;
cout << "Employee Payrate: ";
cin >> emp.payRate;
cout << "Enter the hours worked by this employee: ";
cin >> emp.hours;
cout << "Enter employee's Address (Street) ";
cin >> emp.emp_addr.Street;
cout << "Enter employee's Address (City) ";
cin >> emp.emp_addr.City;
cout << "Enter employee's Address (State) ";
cin >> emp.emp_addr.State;
cout << "Enter employee's Address (Zip code) ";
cin >> emp.emp_addr.Zip_code;
return emp;
}
void displayEmployeeInfo(struct EmployeeInfo emp){
cout << fixed << showpoint << setprecision(2);
cout << "Name: " << emp.name << endl;
cout << "ID: " << emp.empId << endl;
cout << "Payrate: " << emp.payRate << endl;
cout << "Hours worked: " << emp.hours << endl;
cout << "Gross pay: " << emp.hours * emp.payRate << endl;
cout << "Employee's Address (Street): " << emp.emp_addr.Street << endl;
cout << "Employee's Address (City): " << emp.emp_addr.City << endl;
cout << "Employee's Address (State): " << emp.emp_addr.State << endl;
cout << "Employee's Address (Zip code): " << emp.emp_addr.Zip_code << endl;
}
int main()
{
//EmployeeRec employee1 = {"Betty Ross", 14001, 18.75};
//EmployeeRec employee2 = {"Rashid Wallace", 14002, 17.50};
EmployeeRec employee1; //Added lines
EmployeeRec employee2; //Added lines
EmployeeRec empList[100];
int num_employees;
double grossPay;
/*--------------------Part1------------------------*/
cout << "Name: ";
cin >> employee1.name;
cout << "Employee Number: ";
cin >> employee1.empId;
cout << "Employee Payrate: ";
cin >> employee1.payRate;
cout << "Enter the hours worked by this employee: ";
cin >> employee1.hours;
cout << "Name: ";
cin >> employee2.name;
cout << "Employee Number: ";
cin >> employee2.empId;
cout << "Employee Payrate: ";
cin >> employee2.payRate;
cout << "Enter the hours worked by this employee: ";
cin >> employee2.hours;
grossPay = employee1.hours * employee1.payRate;
cout << "Gross Pay: " << grossPay << endl << endl;
grossPay = employee2.hours * employee2.payRate;
cout << "Gross Pay: " << grossPay << endl;
/*--------------------Part1------------------------*/
/*--------------------Part2------------------------*/
//cout << fixed << showpoint << setprecision(2);
employee1 = getEmployeeInfo();
employee2 = getEmployeeInfo();
grossPay = employee1.hours * employee1.payRate;
cout << "Gross Pay: " << grossPay << endl << endl;
grossPay = employee2.hours * employee2.payRate;
cout << "Gross Pay: " << grossPay << endl;
displayEmployeeInfo(employee1);
displayEmployeeInfo(employee2);
/*--------------------Part2------------------------*/
/*--------------------Part3------------------------*/
cout << "Enter number of employees:";
cin >> num_employees;
for (int i =0; i<num_employees; i++){
emplList[i] = getEmployeeInfo();
}
for (int i =0; i<num_employees; i++){
displayEmployeeInfo(emplList[i]);
}
/*--------------------Part3------------------------*/
/*--------------------Part4 (Has been implemented above)------------------------*/
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.