Question

In: Computer Science

Consider a text file that you will create named “employees.txt”. The file contains data organized according...

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

Solutions

Expert Solution

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.


Related Solutions

An HTML file is a text file that contains text to be displayed in a browser...
An HTML file is a text file that contains text to be displayed in a browser and __________ to be interpreted (read) by the browser formatting and styling the document Both C++ and javascript are computer programing languages; what are their differences? What HTML tag can let you insert javascript in to document Which attributes of the <script> tag tells the brorwser what kind of scripting language is insterted? (give an example) in the javascript section of the HTML file,...
In this PYTHON 3 program assignment, you will find a text file named WorldSeries.txt. This file...
In this PYTHON 3 program assignment, you will find a text file named WorldSeries.txt. This file contains a chronological list of the World Series' winning teams from 1903 through 2018. The first line in the file is the name of the team that won in 1903, and the last line is the name of the team that won in 2018. (Note the World Series was not played in 1904 and 1994. There are entries in the file indicating this.) Write...
how to creat Create a text file that contains a list of integer numbers separated by...
how to creat Create a text file that contains a list of integer numbers separated by comma (numbers.txt). E.g. 3, 7, 5, 1, 11, 8,2, 6 2- how to Write a python program (minMax.py) to read the file numbers.txt and find the largest and smallest numbers in the file. (without using use min(), max()). calculate the average and output the results. 3-Use python file i/o, write a python program (fileCopy.py) that makes a copy of minMax.py (Read contents of minMax.cp...
Create a file named StudentArrayList.java,within the file create a class named StudentArrayList. This class is meant...
Create a file named StudentArrayList.java,within the file create a class named StudentArrayList. This class is meant to mimic the ArrayList data structure. It will hold an ordered list of items. This list should have a variable size, meaning an arbitrary number of items may be added to the list. Most importantly this class should implement the interface SimpleArrayList provided. Feel free to add as many other functions and methods as needed to your class to accomplish this task. In other...
Create C# code that can search a text file and output the data at the line...
Create C# code that can search a text file and output the data at the line number inputted and amount of entries needed. Example of call in command window: Search16s filename.txt 273   10 Where 273 is the line number to start the output from, and 10 is the number of sequences that the program should output. The number of sequences entered on call should always be a odd number or give an error in console. The output should also display...
Write a C++ program to create a text file. Your file should contain the following text:...
Write a C++ program to create a text file. Your file should contain the following text: Batch files are text files created by programmer. The file is written in notepad. Creating a text file and writing to it by using fstream: to write to a file, you need to open thew file as write mode. To do so, include a header filr to your program. Create an object of type fsrteam. Open the file as write mode. Reading from a...
Define a class named Document that contains an instance variable of type String named text that...
Define a class named Document that contains an instance variable of type String named text that stores any textual content for the document. Create a method named toString that returns the text field and also include a method to set this value. Next, define a class for Email that is derived from Document and includes instance variables for the sender, recipient, and title of an email message. Implement appropriate set and get methods. The body of the email message should...
Create a class named Horse that contains the following data fields: name - of type String...
Create a class named Horse that contains the following data fields: name - of type String color - of type String birthYear - of type int Include get and set methods for these fields. Next, create a subclass named RaceHorse, which contains an additional field, races (of type int), that holds the number of races in which the horse has competed and additional methods to get and set the new field. ------------------------------------ DemoHorses.java public class DemoHorses {     public static void...
Create a class named Sandwich that contains the following data fields: MainIngredient - of type String...
Create a class named Sandwich that contains the following data fields: MainIngredient - of type String Bread - of type String Price - of type Double Include get and set methods for these fields. The methods should be prefixed with 'get' or 'set' respectively, followed by the field name using camel case. For example, setMainIngredient. Use the application named TestSandwich that instantiates one Sandwich object and demonstrates the use of the set and get methods to test your class. ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------...
Consider the data contained in the Excel worksheet named Cell Phones. This data set contains monthly...
Consider the data contained in the Excel worksheet named Cell Phones. This data set contains monthly phone bills (in dollars) for a random sample of 50 cell phone users. a. Use the data set to conduct a one sample t-test to determine whether the expected bill amount equals $65 or is greater than $65. What is the p-value associated with the test? b. Construct a 95% confidence interval on expected bill amount. c. Do the data provide sufficient evidence to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT