COSC 1436
Lab Chapter 4
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.
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
You should be able to do this fairly easily as well.
You don't even need an array/vector/whatever. Just the ability to write a few loops and read integers from file. A function or two would be nice as well, but not actually necessary (but still recommended).