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,...
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...
Using Python. A file exists on the disk named students.txt. The file contains several records, and...
Using Python. A file exists on the disk named students.txt. The file contains several records, and each record contains two fields: (1) the student’s name, and (2) the student’s score for the final exam. Write code that deletes the record containing “John Perz”as the student name. This the code i have so far but it's not working: import os def main(): found=False search='John Perz' student_file = open('student.txt','r') temp_file = open('temp_students.txt','w') name=student_file.readline() score='' while name !='': score=student_file.readline() name=name.rstrip('/n') score=score.rstrip('/n') if name...
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...
Exercise 2: You are provided with a text file named covid19-3.txt. It reports a few confirmed...
Exercise 2: You are provided with a text file named covid19-3.txt. It reports a few confirmed cases of covid19. It consists of three columns. The 1st column indicates the names of the provinces, the 2nd column indicates the names of the countries and the 3rd column indicates the numbers of confirmed cases. To do: 1. Define a function that reads, from covid19-3.txt, provinces, countries and confirmed cases in three separate lists. 2. Define a function that iterates through a list...
Create a file named Good1 nano Good1 Type Welcome in the file and save it. What...
Create a file named Good1 nano Good1 Type Welcome in the file and save it. What command gives you a long listing of your filenames in the current directory, including permissions attached to each file. What command would you use to change a file’s permissions to include read, write and execute permission for the owner of the file only. Explain the following file permissions    a) 777          b) 765          c) 400          d) 666          e) 600         ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT