In: Computer Science
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
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.