please write the program in c++ and read the instructions carefully Here is the
ID: 3730180 • Letter: P
Question
please write the program in c++ and read the instructions carefully
Here is the project 4 that was previously done
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
//Prototype functions
double calcWage();
void getEmployeeInfor();
void printWages();
void printTable();
void loop();
//Defining terms for the program.
int id,result;
double payRate, hours, wage;
ofstream outputFile;
//ifstream inputFile;
string file,strings;
int main()
{
//Initally get the filename to save the final output to this file
cout << "Please enter the name of the file you wish to save to: ";
cin >> file;
outputFile.open(file.c_str()); //open the file
//Get employee information
getEmployeeInfor();
//Calculate the total wage
wage = calcWage();
//Prints the Table to the outputFile
printTable();
//Prints the wages to the output File
printWages();
//Ask if more informations wants to be added/ calculated
loop();
return 0;
}
//Function for asking user for Employee Id, pay rate, and the hours worked
void getEmployeeInfor()
{
cout << "Please enter your employee id, payrate, and hours worked: " << endl;
cin >> id >> payRate >> hours;
//Loop to check for valid numbers.
while ((id < 0) || payRate < 0 || hours < 0)
{
cout << "Please enter valid numbers (Positive numbers only): ";
cin >> id >> payRate >> hours;
}
}
//Function for calculating the Wage
double calcWage()
{
double wages;
return wages = payRate * hours;
}
//Function for printing information to the outputFile
void printWages()
{
outputFile << setw(8) << id << setprecision(2) << fixed << setw (16) << payRate
<< setprecision(2) << fixed << setw (12) << hours
<< setprecision(2) << fixed << setw (18) << wage << endl;
}
//Function to print table to the outputFile
void printTable()
{
outputFile << setw(8) << "ID" << setw (16) << "HourlyRate" << setw(12) << "Hours"
<< setw(18) << "Wage"<<endl;
outputFile << "------------------------------------------------------ ";
}
//Function that asks the user if the process wants to be repeated
void loop()
{
char answer;
cout << "Do you want to calculate another employe's gross wage? (y/n): ";
cin >> answer;
if (answer == 'y'|| answer == 'Y')
{
getEmployeeInfor(); // gets employee information
wage = calcWage(); // calculates wage
printWages(); // prints wage to the outputfile
//calls loop function again to get an Y or N;
loop();
}
else
{
//Close the outputfile and display final message
outputFile.close();
cout << "The result is reported to the file " << file << endl;
}
}
Explanation / Answer
//header files
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
//function prototypes
void printTable();
void printWages();
void Display_Info(); //to display employee information on screen
void Display_order_ID(); //to display employee information according to order of ID
void Display_order_Rate(); //to display employee information according to order of hourly rate
void Display_order_Worked(); //to display employee information according to order of hours worked
void Display_order_Wage(); //to display employee information according to order of wage
struct Employee{
double rate, hours, wage;
int id;
}emp[100]; //restricting numner of employees in database to 100 as limit is not described
ofstream outputFile;
ifstream inputFile;
int counter = 0; //to keep track of how many employees have been input
int main(){
//Initally get the filename to save the final output to this file
string file;
cout << "Please enter the name of the file you wish to save to: ";
cin >> file;
outputFile.open(file.c_str()); //open the file
cout<<endl;
cout << setfill(' ') << setw(50);
cout << "-- XXXXXX Payroll System --" ;
cout << endl;
cout << setfill(' ') << setw(47);
cout << "Build Employee Database";
cout << endl << endl;
char add_entry; //loop control variable for input of more that one employee
int i = 0;
do{
cout << " Enter an employee's information by order of ID number, rate, hours: ";
cin >> emp[i].id >> emp[i].rate >> emp[i].hours;
//Checking for valid input
while ((emp[i].id < 0) || emp[i].rate < 0 || emp[i].hours < 0){
cout << "Please enter valid numbers (Positive numbers only): ";
cin >> emp[i].id >> emp[i].rate >> emp[i].hours;
}
emp[i].wage = emp[i].rate*emp[i].hours;
i++;
counter++;
cout << "Do you want to calculate another employee's gross wage? ";
cin >> add_entry;
}while(add_entry=='y');
//calling functions to print employee data in output file
printTable();
printWages();
cout << " The result was reported to the file "" << file << "" ";
// Code section for the various outputs
int choice;
cout<<endl;
do{
cout << setfill(' ') << setw(50);
cout << "-- XXXXXX Payroll System --" ;
cout << endl;
cout << setfill(' ') << setw(47);
cout << "Display Employee Data";
cout << endl << endl;
cout << setfill(' ') << setw(15) << "1. Display employees' information to screen ";
cout << setfill(' ') << setw(15) << "2. Display employees' information to screen according to the order of ID ";
cout << setfill(' ') << setw(15) << "3. Display employees' information to screen according to the order of hourly rate ";
cout << setfill(' ') << setw(15) << "4. Display employees' information to screen according to the order of hours worked ";
cout << setfill(' ') << setw(15) << "5. Display employees' information to screen according to the order of wage ";
cout << setfill(' ') << setw(15) << "6. Quit the system ";
cout << setfill(' ') << setw(1) << "Enter your option --> ";
cin >> choice;
switch(choice){
case 1:
Display_Info();
break;
case 2:
Display_order_ID();
break;
case 3:
Display_order_Rate();
break;
case 4:
Display_order_Worked();
break;
case 5:
Display_order_Wage();
break;
case 6:
cout << "Thank you for using XXXXXX Payroll System!";
break;
default:
cout<<"Invalid Input! Try Again";
break;
}
}while(choice!=6);
outputFile.close(); //closing output file after adding information
return 0;
}
void printTable(){
outputFile << setw(8) << "ID" << setw (16) << "HourlyRate" << setw(12) << "Hours"
<< setw(18) << "Wage"<<endl;
outputFile << "------------------------------------------------------ ";
}
void printWages(){
for (int i=0;i<counter;i++){
outputFile << setw(8) << emp[i].id << setprecision(1) << fixed << setw (16) << emp[i].rate
<< setprecision(1) << fixed << setw (12) << emp[i].hours
<< setprecision(1) << fixed << setw (18) << emp[i].wage << endl;
}
}
void Display_Info(){
cout << setw(8) << "ID" << setw (16) << "HourlyRate" << setw(12) << "Hours"
<< setw(18) << "Wage"<<endl;
for(int i=0;i<counter;i++){
cout << setw(8) << emp[i].id << setprecision(1) << fixed << setw (16) << emp[i].rate
<< setprecision(1) << fixed << setw (12) << emp[i].hours
<< setprecision(1) << fixed << setw (18) << emp[i].wage << endl;
}
cout<<endl;
}
void Display_order_ID(){
//ordering items using bubble sort on the ID field
for(int i=0; i<counter; i++){
for(int j=0; j<counter-i-1;j++){
if(emp[j].id > emp[j+1].id){
Employee temp = emp[j];
emp[j] = emp[j+1];
emp[j+1] = temp;
}
}
}
cout << setw(8) << "ID" << setw (16) << "HourlyRate" << setw(12) << "Hours"
<< setw(18) << "Wage"<<endl;
for(int i=0;i<counter;i++){
cout << setw(8) << emp[i].id << setprecision(1) << fixed << setw (16) << emp[i].rate
<< setprecision(1) << fixed << setw (12) << emp[i].hours
<< setprecision(1) << fixed << setw (18) << emp[i].wage << endl;
}
cout<<endl;
}
void Display_order_Rate(){
//ordering items using bubble sort on the rate field
for(int i=0; i<counter; i++){
for(int j=0; j<counter-i-1;j++){
if(emp[j].rate > emp[j+1].rate){
Employee temp = emp[j];
emp[j] = emp[j+1];
emp[j+1] = temp;
}
}
}
cout << setw(8) << "ID" << setw (16) << "HourlyRate" << setw(12) << "Hours"
<< setw(18) << "Wage"<<endl;
for(int i=0;i<counter;i++){
cout << setw(8) << emp[i].id << setprecision(1) << fixed << setw (16) << emp[i].rate
<< setprecision(1) << fixed << setw (12) << emp[i].hours
<< setprecision(1) << fixed << setw (18) << emp[i].wage << endl;
}
cout<<endl;
}
void Display_order_Worked(){
//ordering items using bubble sort on the hours field
for(int i=0; i<counter; i++){
for(int j=0; j<counter-i-1;j++){
if(emp[j].hours > emp[j+1].hours){
Employee temp = emp[j];
emp[j] = emp[j+1];
emp[j+1] = temp;
}
}
}
cout << setw(8) << "ID" << setw (16) << "HourlyRate" << setw(12) << "Hours"
<< setw(18) << "Wage"<<endl;
for(int i=0;i<counter;i++){
cout << setw(8) << emp[i].id << setprecision(1) << fixed << setw (16) << emp[i].rate
<< setprecision(1) << fixed << setw (12) << emp[i].hours
<< setprecision(1) << fixed << setw (18) << emp[i].wage << endl;
}
cout<<endl;
}
void Display_order_Wage(){
//ordering items using bubble sort on the wage field
for(int i=0; i<counter; i++){
for(int j=0; j<counter-i-1;j++){
if(emp[j].wage > emp[j+1].wage){
Employee temp = emp[j];
emp[j] = emp[j+1];
emp[j+1] = temp;
}
}
}
cout << setw(8) << "ID" << setw (16) << "HourlyRate" << setw(12) << "Hours"
<< setw(18) << "Wage"<<endl;
for(int i=0;i<counter;i++){
cout << setw(8) << emp[i].id << setprecision(1) << fixed << setw (16) << emp[i].rate
<< setprecision(1) << fixed << setw (12) << emp[i].hours
<< setprecision(1) << fixed << setw (18) << emp[i].wage << endl;
}
cout<<endl;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.