help me. Im new. Write cpp

COSC 1436
Lab Chapter 4
Here we will be taking data in as a file. 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:
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

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 month 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 have to have made their hours. But the last week may not have 5 full days in it. For the last week you must prorate 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 30 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:
NORMAL
DOCK 2
NORMAL
OVERTIME 3
NORMAL

Final submission should be your .cpp file only (as again I will use my own txt file to grade your assignment).


I try this one but not really work.
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream inputfile;
inputfile.open("Person.txt");
int a, b, c, d, e;
int i = 6
char output[100];
int i = 0, j = 0, lineCount = 1, minHrs=0, maxHrs = 0, hrsWorked = 0, totHrs = 0;
int dock = 0, overTime = 0; // 0 = false, 1 = true;
int dockHrs = 0, overTimeHrs = 0, numOfDays = 0;
// minimum of pro rata 7 hours per day ( hence minimu 35 for 5 days)
// maximum of pro rata 9 hours per day (hence maximum of 45 for 5 days)
//fgets( line, 80, ifstream); // read the first line
if (lineCount == 1) {
cout << "\n Minimum Hours = " << minHrs << "Maximum Hours = " << maxHrs;
cout << "Line count = " << lineCount; // how many lines
}
// in our example minHr = 35, maxHr = 45 it may vary depending up on the data in the input text file
// after reading the first line, there will be five more lines of the data
for (i = 1; i <= 5; i++) {
// re initialize the dock, totHrsWorked and overTime variables
dock = 0; overTime = 0; totHrs = 0;
for (j = 1; j <= numOfDays; j++) { // read as many days as the numOfDays
totHrs = totHrs + hrsWorked;
} // end inner for
if ((totHrs / numOfDays) < 7) dock = 1; // decide whether less than minimum
else if ((totHrs / numOfDays) > 9) overTime = 1; // or more than maximum
if (dock == 1) {
dockHrs = minHrs - totHrs; // 35 - 30 = 5 hrs of dock
cout << "\n DOCK " << dockHrs;
}// end if
else if (overTime == 1) {
overTimeHrs = totHrs - maxHrs; // 49 - 45 = 4 hours of over time
cout << "\n OVERTIME " << overTimeHrs;
} // end if
else cout << "\n NORMAL ";


} // end outer for loop
lineCount++;
//if( feof(ifstream) ) { break ; }
cout << "\n There are " << lineCount << " lines in the data file";
system("Pause");
return 0 ;
}
Topic archived. No new replies allowed.