In: Computer Science
Consider a text file that you will create named “employees.txt”. The file contains data organized according to the following format:John Smith 10 15Sarah Johnson 40 12Mary Taylor 27 13Jim Stewart 25 8For instance, “John” is the first name, “Smith” is the last name, “10” is the number of hours per week, and “15” is 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 named “results.txt”. ‘results.txt” must be created automatically by your program. You must use a function to read/process the data from the input file and this function must have parameters. The “employees.txt” file must be opened from main and read from within your defined function. in c++
and how to do the screenshots on mac
Please look at my code and in case of indentation issues check the screenshots.
---------------main.cpp----------------
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
struct employee{ //create a structure to store employee
details
string fname, lname;
int hours_per_week, hourly_pay;
int weekly_pay;
};
int read_data(ifstream &inpfile, employee array[]);
void calculate_weekly_pay(employee array[], int size);
void write_to_file(employee array[], int size);
int main()
{
employee array[100]; //create an employee array to store employee
details
ifstream inpfile("employees.txt"); //open input file
int n = read_data(inpfile, array); //read data from file and store
results into array
calculate_weekly_pay(array, n); //calculate the weekly pay of each
employee
write_to_file(array, n); //write the results into file
cout << "results.txt created successfully\n";
return 0;
}
int read_data(ifstream &inpfile, employee array[])
{
int i = 0; //array index
//read each line from file, and store details into array in the
structure fields
while(inpfile >> array[i].fname >> array[i].lname
>> array[i].hours_per_week >>
array[i].hourly_pay){
i++; //move to next index of array
}
return i; //return the number of employees read from file
}
void calculate_weekly_pay(employee array[], int size)
{
for(int i = 0; i < size; i++) //loop for each index of
array
{
array[i].weekly_pay = array[i].hours_per_week *
array[i].hourly_pay; //calculate weekly pay
}
}
void write_to_file(employee array[], int size)
{
ofstream outfile("results.txt"); //open output file for
writing
outfile << "First Name\tLast name\tWeekly Salary\n"; //write
all employees details
for(int i = 0; i < size; i++) //setw is used for allignment in
the output, it sets the width
{
outfile << left << setw(14) << array[i].fname
<< setw(10) << array[i].lname << setw(5) <<
array[i].weekly_pay << endl;
}
}
--------------Screenshots--------------------
------------------Input------------------
------------------Output------------------
----------------------------------------------------------------------------------------------------------
Please give a thumbs up if you find this answer helpful.
If it doesn't help, please comment before giving a thumbs
down.
Please Do comment if you need any clarification.
I will surely help you.