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 (as again I will use my own txt file to grade your assignment). Do not submit project files or solution files.

i need help with this code please

Solutions

Expert Solution

#include<iostream>
#include<fstream>
using namespace std;
int main()
{
   ifstream ifile;
  
   int minwrkhr,maxwrkhr,totwrkhr,wrkhr,i,weakdays,weakno; //variable declaration
  
   int weaktot[5][2]; //array to save total weak work hours
      
   ifile.open("Person.txt"); //open input Person.txt file
  
   if(!ifile)
   {
       cerr<<"Can't opne input file";
       return 0;
   }
  
   //read minimum and maximum working hour
   ifile>>minwrkhr;
   ifile>>maxwrkhr;


//read all days working hour and save total working hours of weak in array weaktot
weakno=0;
   while(ifile>>weakdays)
   {
       totwrkhr = 0;
       wrkhr = 0;
      
       for (i=1 ; i<=weakdays ; i++)
       {
           ifile>>wrkhr;
           totwrkhr += wrkhr;
       }
       weaktot[weakno][0]=weakdays;
       weaktot[weakno][1]=totwrkhr;
      
       weakno++;  
   }
  
   ifile.close(); //close input file
  
   //read working hours of each weak
  
   for(i=0;i<weakno;i++)
   {
  
   if(i == (weakno-1) && weaktot[i][0]<5) //to check last weak and weak days less < 5
       {
           minwrkhr = minwrkhr *weaktot[i][0] / 5; //calculate minimum working hours on prorate basis for last weak
               if(weaktot[i][1] > minwrkhr) //if total working hours greater than maximum working hours
   cout<<"OVERTIME "<<weaktot[i][1]- minwrkhr<<endl;
       else
       cout<<"NORMAL"<<endl;
       }
      
       else if(weaktot[i][1] > maxwrkhr) //if total working hours greater than maximum working hours
   cout<<"OVERTIME "<<weaktot[i][1] - maxwrkhr<<endl;
   else if(weaktot[i][1] < minwrkhr) //if total working hours less than minimum working hours
       cout<<"DOCK "<<minwrkhr - weaktot[i][1]<<endl;
       else
       cout<<"NORMAL"<<endl;
   }
  
   return 0; //end of main
}


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...
Here we will be taking data in as a file. Note that the sample file provided...
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. The file you will be imputing to your program is a grade sheet. You will be taking in the grade sheet of 5 students with an unknown amount of grades (the number of possible grades is from 2 to 8). The number of grades...
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...
B. Write a program Median.java to read each file whose name is specified in the command-line...
B. 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...
**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...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT