Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help with this c++ problem Objectives Read data from a text file Write da

ID: 3878156 • Letter: I

Question

I need help with this c++ problem

Objectives

Read data from a text file

Write data to a text file

Correctly end a file input loop when end of file is reached

Process control breaks correctly

Calculate sums for grouping of input data

Implement an algorithm

Format output data on a report neatly

Test and debug a program

Overview

This assignment involves the creation of a level break report. The data file provided has already been put into the proper sorted order. The format of each record is as follows:

department: String

employee id: String

month: int

day: int

year: int

hours: double

The data file is also available here: cb1data.txt.

Requirements

Your task is to read the records in from the data file (datafile.txt) and produce a report to a file named filereport.txt.

Although the following technique is "brittle", you may read the data from the input file using the standard input extraction operator. For example, if you wanted to read one whole record in one statement and had the appropriate variables declared, you could write:
infile >> dept >> empID >> month >> day >> year >> hours;

Sample Output

Daily hours worked by Department by Employee

Dept EmpID Date Hours

IT D4772 1/02/2011 8.25
IT D4772 1/03/2011 7.00
IT D4772 1/05/2011 9.25
IT D4772 1/06/2011 9.00
IT D4772 1/08/2011 7.25
Employee total 40.75

IT F9642 1/04/2011 8.25
IT F9642 1/06/2011 7.25
IT F9642 1/07/2011 5.25
IT F9642 1/08/2011 8.00
Employee total 28.75

IT V1001 1/03/2011 5.25
IT V1001 1/04/2011 8.50
IT V1001 1/05/2011 6.75
IT V1001 1/06/2011 6.75
IT V1001 1/07/2011 8.00
IT V1001 1/08/2011 6.00
Employee total 41.25

Department total 110.75

MGT A0010 1/02/2011 6.50
MGT A0010 1/03/2011 8.25
MGT A0010 1/04/2011 9.25
MGT A0010 1/05/2011 9.75
MGT A0010 1/06/2011 5.50
MGT A0010 1/07/2011 8.75
MGT A0010 1/08/2011 8.75
Employee total 56.75

MGT S0812 1/04/2011 5.25
MGT S0812 1/05/2011 7.75
MGT S0812 1/06/2011 7.25
MGT S0812 1/07/2011 6.00
MGT S0812 1/08/2011 9.50
Employee total 35.75

Department total 92.50

MKT H6554 1/02/2011 9.25
MKT H6554 1/03/2011 8.00
MKT H6554 1/04/2011 5.75
MKT H6554 1/05/2011 8.75
MKT H6554 1/06/2011 8.50
MKT H6554 1/08/2011 6.00
Employee total 46.25

Department total 46.25

MNT E5109 1/02/2011 8.75
MNT E5109 1/03/2011 5.50
MNT E5109 1/04/2011 6.00
MNT E5109 1/06/2011 5.00
MNT E5109 1/08/2011 6.50
Employee total 31.75

Department total 31.75

MSC E4100 1/03/2011 5.75
MSC E4100 1/04/2011 8.75
MSC E4100 1/05/2011 7.25
MSC E4100 1/07/2011 6.50
MSC E4100 1/08/2011 8.75
Employee total 37.00

Department total 37.00

PRD A0132 1/04/2011 9.00
PRD A0132 1/05/2011 8.00
PRD A0132 1/06/2011 8.75
PRD A0132 1/07/2011 9.00
PRD A0132 1/08/2011 5.00
Employee total 39.75

PRD E1231 1/02/2011 6.75
PRD E1231 1/03/2011 8.25
PRD E1231 1/04/2011 6.75
PRD E1231 1/06/2011 8.00
PRD E1231 1/07/2011 5.50
PRD E1231 1/08/2011 7.25
Employee total 42.50

PRD E1250 1/02/2011 9.75
PRD E1250 1/03/2011 7.75
PRD E1250 1/04/2011 7.75
PRD E1250 1/05/2011 7.50
Employee total 32.75

Department total 115.00

SLS L1776 1/02/2011 9.00
SLS L1776 1/03/2011 7.75
SLS L1776 1/04/2011 6.75
SLS L1776 1/06/2011 5.50
SLS L1776 1/08/2011 8.75
Employee total 37.75

SLS M5205 1/02/2011 7.00
SLS M5205 1/03/2011 5.00
SLS M5205 1/04/2011 7.75
SLS M5205 1/05/2011 6.00
SLS M5205 1/06/2011 7.75
SLS M5205 1/07/2011 5.25
SLS M5205 1/08/2011 8.25
Employee total 47.00

Department total 84.75

Total hours worked 518.00

70 records processed

Pseudocode

open input file for sequential access
open output (report) file
print headings to report file

totHours = empHours = deptHours = 0
prevID = prevDept = "$$$"
recno = 0

while there are still more records in the input file
get the next record from the input file
recno = recno + 1
if record's dept is different than prevDept and recno > 1
// employee level break
print employee's total hours to output file
add empHours to deptHours
empHours = 0
// department level break
print department's total hours to output file
add deptHours to totHours
deptHours = 0
otherwise if record's employee id is different than prevID and recno > 1
// employee level break
print employee's total hours to output file
add empHours to deptHours
empHours = 0
print formatted record detail to output file
add record's hours to empHours
prevID = record's employee id
prevDept = record's department

close input file

// employee level break
print employee's total hours to output file
add empHours to deptHours
empHours = 0

// department level break
print department's total hours to output file
add deptHours to totHours
deptHours = 0

print grand totals to output file
print footers to output file
close output file
end

datafile.txt

Explanation / Answer

Program:--

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
   ifstream infile ("datafile.txt");   //open txt file
   ofstream outfile ("report.txt");
    float totHours = 0, empHours = 0, deptHours = 0, hours;
   string dept, empID, prevID = "$$$", prevDept = "$$$";
   int recno = 0, month, day, year;
   outfile<<"Dept EmpID     Date     Hours"<<endl;
    while (infile >> dept >> empID >> month >> day >> year >> hours)   //read from file and assign values to a,b,c
    {
       recno = recno + 1;
       if (dept!=prevDept && recno>1) //if record's dept is different than prevDept and recno > 1
        {   // employee level break
           outfile<<"Employee Total "<<empHours<<endl<<endl;   //print employee's total hours to output file
           deptHours+=empHours;//   dadd empHours to deptHours
           empHours = 0;
           // department level break
           outfile<<"Department Total   "<<deptHours<<endl<<endl; //print department's total hours to output file
           totHours+=deptHours;   //add deptHours to totHours
           deptHours = 0;
       }
       else if (empID != prevID && recno>1)   //otherwise if record's employee id is different than prevID and recno > 1
        {// employee level break
           outfile<<"Employee Total "<<empHours<<endl<<endl;   //print employee's total hours to output file
           deptHours+=empHours;   //add empHours to deptHours
           empHours = 0;
       }  
       outfile<<dept<<"   "<<empID<<"   "<<month<<"/"<<day<<"/"<<year<<"   "<<hours<<endl;   //print formatted record detail to output file
       empHours+=hours;   //add record's hours to empHours
       prevID=empID;   //prevID = record's employee id
       prevDept=dept;   //prevDept = record's department
   }
   infile.close();//close input file

   // employee level break
   outfile<<"Employee Total "<<empHours<<endl<<endl;   //print employee's total hours to output file
   deptHours+=empHours;   //add empHours to deptHours
    empHours = 0;

   // department level break
   outfile<<"Department Total   "<<deptHours<<endl<<endl; //print department's total hours to output file
    totHours+=deptHours ;   //add deptHours to totHours
    deptHours = 0;
  
   outfile<<"Total hours worked "<<totHours<<endl;   //print grand totals to output file
   outfile<<recno<<" records processed"<<endl;   //print footers to output file
   outfile.close();   //close output file  
    return 0;
}

output:--

Process exited normally.
Press any key to continue . . .

outputfile:-

Dept EmpID     Date     Hours
IT   D4772   1/2/2011   8.25
IT   D4772   1/3/2011   7
IT   D4772   1/5/2011   9.25
IT   D4772   1/6/2011   9
IT   D4772   1/8/2011   7.25
Employee Total       40.75

IT   F9642   1/4/2011   8.25
IT   F9642   1/6/2011   7.25
IT   F9642   1/7/2011   5.25
IT   F9642   1/8/2011   8
Employee Total       28.75

IT   V1001   1/3/2011   5.25
IT   V1001   1/4/2011   8.5
IT   V1001   1/5/2011   6.75
IT   V1001   1/6/2011   6.75
IT   V1001   1/7/2011   8
IT   V1001   1/8/2011   6
Employee Total       41.25

Department Total   110.75

MGT   A0010   1/2/2011   6.5
MGT   A0010   1/3/2011   8.25
MGT   A0010   1/4/2011   9.25
MGT   A0010   1/5/2011   9.75
MGT   A0010   1/6/2011   5.5
MGT   A0010   1/7/2011   8.75
MGT   A0010   1/8/2011   8.75
Employee Total       56.75

MGT   S0812   1/4/2011   5.25
MGT   S0812   1/5/2011   7.75
MGT   S0812   1/6/2011   7.25
MGT   S0812   1/7/2011   6
MGT   S0812   1/8/2011   9.5
Employee Total       35.75

Department Total   92.5

MKT   H6554   1/2/2011   9.25
MKT   H6554   1/3/2011   8
MKT   H6554   1/4/2011   5.75
MKT   H6554   1/5/2011   8.75
MKT   H6554   1/6/2011   8.5
MKT   H6554   1/8/2011   6
Employee Total       46.25

Department Total   46.25

MNT   E5109   1/2/2011   8.75
MNT   E5109   1/3/2011   5.5
MNT   E5109   1/4/2011   6
MNT   E5109   1/6/2011   5
MNT   E5109   1/8/2011   6.5
Employee Total       31.75

Department Total   31.75

MSC   E4100   1/3/2011   5.75
MSC   E4100   1/4/2011   8.75
MSC   E4100   1/5/2011   7.25
MSC   E4100   1/7/2011   6.5
MSC   E4100   1/8/2011   8.75
Employee Total       37

Department Total   37

PRD   A0132   1/4/2011   9
PRD   A0132   1/5/2011   8
PRD   A0132   1/6/2011   8.75
PRD   A0132   1/7/2011   9
PRD   A0132   1/8/2011   5
Employee Total       39.75

PRD   E1231   1/2/2011   6.75
PRD   E1231   1/3/2011   8.25
PRD   E1231   1/4/2011   6.75
PRD   E1231   1/6/2011   8
PRD   E1231   1/7/2011   5.5
PRD   E1231   1/8/2011   7.25
Employee Total       42.5

PRD   E1250   1/2/2011   9.75
PRD   E1250   1/3/2011   7.75
PRD   E1250   1/4/2011   7.75
PRD   E1250   1/5/2011   7.5
Employee Total       32.75

Department Total   115

SLS   L1776   1/2/2011   9
SLS   L1776   1/3/2011   7.75
SLS   L1776   1/4/2011   6.75
SLS   L1776   1/6/2011   5.5
SLS   L1776   1/8/2011   8.75
Employee Total       37.75

SLS   M5205   1/2/2011   7
SLS   M5205   1/3/2011   5
SLS   M5205   1/4/2011   7.75
SLS   M5205   1/5/2011   6
SLS   M5205   1/6/2011   7.75
SLS   M5205   1/7/2011   5.25
SLS   M5205   1/8/2011   8.25
Employee Total       47

Department Total   84.75

Total hours worked 518
70 records processed