Question

In: Computer Science

Here we will be taking data in as a file instead of from the command line...

Here we will be taking data in as a file instead of from the command line (no cin). Note that the sample file provided here is not the file that I will use to grade your assignment but the formatting will be the same.

File input is very similar to how data comes in from the user through cin (using the same operators) but we will need to setup the stream ourselves.

First we need to include <fstream> at the top of our file.

We will need to create an input file stream to work from now. We will do this with ifstream. ifstream is a datatype just like int or float. Create a variable with the datatype being ifstream. We will define the variable by using the member accessor operator to call the open function and passing in “Person.txt”, which is where our data will be.

Example: [whatever the variable name is].open(“Person.txt”);

Your program must be setup to open “Person.txt” as the file. For testing you can add your own Person.txt file into visual studios as a resource.

The file your program will have to take in will be formatted like so:

The first 2 numbers are the min and max that the Person can work. Anything under the min and the employee is docked salary. Anything above and the employee must be paid overtime. These number may vary but min will always be less than the max.

There will then be 5 lines with different amounts of numbers (indicating the Person’s work times per line):

The first number will tell how many numbers will follow (since some weeks do not have 5 work days and can have holidays.

Example file 1:

35 45

5 8 8 7 9 8

4 10 8 2 13

6 4 8 10 9 8 1

5 8 10 10 10 10

3 8 7 8

Example file 2:

20 20

3 8 8 4

2 8 8

3 8 8 8

5 8 8 4 4 2

2 4 5

You will read the first number in using the stream extraction operator. Based on that number you can read in the rest of the numbers (again using the stream extraction operator). What you will be trying to identify is whether the Person whose data you read in worked more than their max hours per week - in which case you will indicate this by outputting “OVERTIME” and however much over the max the person worked for that week. Or if the Person worked less than their min you should output “DOCK” and indicate how many hours below min they worked.

The work week will always begin on a 5 day schedule, though some persons may work on the weekend (as much as 7 days). If the person only worked 4 days or less then they will still be held to the same min and max hours. Unless you are on the last week, which may not have 5 full days in it. For the last week you must prorate (get a percentage) according to how many days are indicated. Meaning if there is only 4 days in the last week and the employee normally will work 40 hours per their min then they would only have to work 32 (40 * 4/5 = 32) hours on the last week.

The final output should indicate per week whether they should be “Docked pay”, “Normal pay”, or “Overtime” for each week (Dock and Overtime of course indicating by how much). So the output of the sample above could be:

Example Output 1:

NORMAL

DOCK 2

NORMAL

OVERTIME 3

NORMAL

Example Output 2:

NORMAL

DOCK 4

OVERTIME 4

OVERTIME 6

OVERTIME 1

Final submission should be your .cpp file only

Solutions

Expert Solution

Please find the answer below, all the details are mentioned in the comments.

Code.cpp

#include<iostream>
#include<string>
#include<fstream>

using namespace std;


int main(){
//ifstream is declared to stream the file to get as an input
ifstream in_stream;
//file name for the input
string fileInput = "Person.txt";

//open the file for reading
in_stream.open(fileInput.c_str(), ios::in | ios::binary);

//to store min and max value
int min,max;

//get the min and max value
in_stream >> min;
in_stream >> max;

//different variables to perform operations
int i,j;
int numberOfDays;
int totalHours;
int val;

//for the all weeks except the last one
for(i=1;i<=4;i++){
//get the number of days
in_stream >> numberOfDays;
totalHours = 0;

//loop through the all days value
for(j=1;j<=numberOfDays;j++){
in_stream >> val;
totalHours += val;
}

//based on the hours determine the output
if(totalHours < min){
cout << "DOCK " << (min - totalHours) << endl;
}
else if(totalHours > max){
cout << "OVERTIME " << (totalHours - max) << endl;
}
else{
cout << "NORMAL " << endl;
}
}

//for the last week
in_stream >> numberOfDays;

//modify the min and max value
double changed_min = min * (numberOfDays / 5.0);
double changed_max = max * (numberOfDays / 5.0);
totalHours = 0;

//get total hours sum
for(j=1;j<=numberOfDays;j++){
in_stream >> val;
totalHours += val;
}

//print the message as per the total hours and changed min and max
if(totalHours < changed_min){
cout << "DOCK " << (changed_min - totalHours) << endl;
}
else if(totalHours > changed_max){
cout << "OVERTIME " << (totalHours - changed_max) << endl;
}
else{
cout << "NORMAL " << endl;
}

//close the file
in_stream.close();
return 0;
}

Output:

Sample 1:

Sample 2:

Please let us know in the comments if you face any problems.


Related Solutions

Here we will be taking data in as a file instead of from the command line...
Here we will be taking data in as a file instead of from the command line (no cin). Note that the sample file provided here is not the file that I will use to grade your assignment but the formatting will be the same. File input is very similar to how data comes in from the user through cin (using the same operators) but we will need to setup the stream ourselves. First we need to include <fstream> at the...
In python explain why would we want to read a file line by line instead of...
In python explain why would we want to read a file line by line instead of all at once?
Here are the tasks for this assignment: 1. Write a bash command line to determine if...
Here are the tasks for this assignment: 1. Write a bash command line to determine if the server 'syccuxfs01.pcc.edu' is reachable from the syccuxas01.pcc.edu server. 2. As you have read, TCP/IP messages are passed from one device to another until the message reaches its destination. Write a bash command line that will verify that no more than two (2) network devices are used to pass messages from the syccuxas01.pcc.edu server to the www.pcc.edu server. 3. Write a bash command line...
Of the following, which is a reason we take samples instead of taking a census? Census...
Of the following, which is a reason we take samples instead of taking a census? Census is a hard word to say. A census is not reliable. A census doesn't give a good understanding of a whole group of people. A census is almost impossible to perform. All of the above.
Write a program Median.java to read each file whose name is specified in the command-line arguments....
Write a program Median.java to read each file whose name is specified in the command-line arguments. That is, for each command-line argument, open it as a file and read it. The file contents are zero or more lines each containing a list of comma-separated integers, such as 1,2,3,4 or 99,120,33. You should parse each of these integers and save them in an ArrayList (if you prefer you may use an array, but an ArrayList is likely to be easier for...
**Need to use awk command in putty (should be a ONE LINE COMMAND) Write the command...
**Need to use awk command in putty (should be a ONE LINE COMMAND) Write the command that would find all lines that have an email address and place a label email = before the line in the file longfile output will multiple lines similar to this one : using a good awk command the output would be something like this email = From: "Linder, Jann/WDC" <[email protected]> email = To: Mr Arlington Hewes <[email protected]> email = > From: Mr Arlington Hewes...
1. If we use the command: FSUTIL FILE CREATENEW file1.txt, what other parameter must we include?...
1. If we use the command: FSUTIL FILE CREATENEW file1.txt, what other parameter must we include? File System File Attributes File Size We can use the SC utility tool to manage services, and the following are all options for SC, except: LIST CREATE QUERY STOP /Length When we use DIR without any switches, we will see the following information, except: Group of answer choices Last Modified Time File/Directory Name Ownership File Size While we can use the % for variables...
1. At the command prompt, use the where command to locate the where.exe file. Copy and...
1. At the command prompt, use the where command to locate the where.exe file. Copy and paste the full path and filename for the where.exe file into the space provided below. 2. At the command prompt, enter the following command and press enter. find "free beer" "c:\Program Files (x86)\Notepad++\*.txt" This command searches for the string "free beer" in all .TXT files in the Notepad++ program folder. According to the output of the find command, which of the files listed below...
Assume there is a file called "mydata". each line of the file contains two data items
how do you read in a file in JAVA Assume there is a file called "mydata". each line of the file contains two data items: hours and rate. hours is the represented by the number of hours the worker worked and rate is represented as hourly rate of pay. The first item of data is count indicating how many lines of data are to follow.Methodspay- accepts the number of hours worked and the rate of pay. returns the dollor and cents...
Discuss the pros and cons of an IDE vs command-line interpreters. Why are command-line interpreters still...
Discuss the pros and cons of an IDE vs command-line interpreters. Why are command-line interpreters still being used? Be sure to argue both sides.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT