C++ Program. Lets consider a text file named \"employees.txt.\" The file contain
ID: 644788 • Letter: C
Question
C++ Program. Lets consider a text file named "employees.txt." The file contains data organized according to the following format:
John Smith 20 9
Sarah Johnson 35 12
Mary Taylor 27 11
Jim Stewart 23 8
The numbers closer to the name are hours worked per week. the numbers to the right are the hourly rate. Write a program that computes the weekly salary of each employee. The program prints the first name, last name, and weekly salary of each employee in a file names results.txt
Explanation / Answer
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
struct employee{
string firstName;
string lastName;
int hours;
double hourlyRate;
};
int getInput(employee employees[]){
ifstream in;
in.open("employees.txt");
string name;
int hours, count = 0;
double hRate;
if(in.is_open()){
while(in >> name){
employee temp;
temp.firstName = name;
in >> name;
temp.lastName = name;
in >> hours;
temp.hours = hours;
in >> hRate;
temp.hourlyRate = hRate;
employees[count++] = temp;
}
return count;
}
else{
cout << "Can not find employees.txt in project directory" << endl;
return 0;
}
}
void printOutput(employee employees[], int count){
cout << "First Name" << " Last Name" << " Salary" << endl;
for(int i = 0; i < count; ++i){
double salary = employees[i].hours * employees[i].hourlyRate;
cout << employees[i].firstName << " " << employees[i].lastName << " " << salary << endl;
}
}
int main(){
employee employees[100];
int count;
count = getInput(employees);
if(count != 0){
printOutput(employees, count);
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.