Question

C++ Need help with this code. Can't use "using namespace std;" Control break report Objectives Read...

C++ Need help with this code. Can't use "using namespace std;"

Control break report

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

Requirements

Your task is to read the records in from the data file (cb1data.txt) and produce a report to a file named cbreport.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;

A "priming read" is well-suited for this type of algorithm. A priming read makes a read from input just before a while loop starts, and then has the same read just before the end of the loop body. That type of read works very well when reading from a text file and checking for end-of-file. Instead of trying to read all of the fields from a single record though, it is better to just use the first field for the priming read, and read the remainder of the fields for that record as the first line within the loop. Example:

infile >> dept;
while (infile) {
infile >> empID >> month >> day >> year >> hours;
... other processing code ...
infile >> dept;
}

You have been given an algorithm to implement. There are some ambiguities to simulate a real life situation - but no errors that the instructor is aware of. You are required to use ostream manipulators (left, right, setw, setprecision, fixed, showpoint, etc.) to get the correct output formatting for this project.

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

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

cb1data.txt (below)

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
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
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
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
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
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
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
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
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
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
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
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
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
0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>


int main(){
  
   // input file
   std::ifstream inFile;
     
   // output file
   std::ofstream outFile;
  
   inFile.open("cb1data.txt");
   outFile.open("cbreport.txt");
  
   // to store file data to variables
   std::string dept;
   std::string empID;
   int month;
   int day;
   int year;
   float hours;
  
  
   // store previous dept amd empID
   // for checking
   // new dept or empID for or not
   std::string prevDept;
   std::string prevempID;
  
   float empTotal=0.0;// store employee hours
   float depTotal=0.0;// store department hours
   float tTotal=0.0;// store total hours
  
  
   // print in file
   outFile<<"Daily hours worked by Department by Employee\n";
   outFile<<"Dept EmpID Date Hours\n";
  
   // print
   std::cout<<"Daily hours worked by Department by Employee\n";
   std::cout<<"Dept EmpID Date Hours\n";
  
   // read fisrt line from file
  
   inFile>>dept;
   inFile>>empID;
   inFile>>month;
   inFile>>day;
   inFile>>year;
   inFile>>hours;
  
  
   // add employee hours
   empTotal+=hours;
  
   // print in file
   outFile<<dept<<" ";
   outFile<<empID<<" ";
   outFile<<month<<"/";
   outFile<< std::setw(2) << std::setfill('0') <<day<<"/";
   outFile<<year<<" ";
   outFile<< std::setprecision(2) << std::fixed<<hours<<"\n";
  
   // print
   std::cout<<dept<<" ";
   std::cout<<empID<<" ";
   std::cout<<month<<"/";
   std::cout<< std::setw(2) << std::setfill('0') << day<<"/";
   std::cout<<year<<" ";
   std::cout<< std::setprecision(2) << std::fixed<<hours<<"\n";
  
   // to count total number of data
   int count=1;  
  
  
  
   // raed all remaining data from file
  
   while(!inFile.eof()){
       count++;
      
       // store previous information
      
       prevDept=dept;
       prevempID=empID;
      
      
       inFile>>dept;
       inFile>>empID;
       inFile>>month;
       inFile>>day;
       inFile>>year;
       inFile>>hours;
      
      
       // if employee id changed , then print and reset
      
       if(prevempID!=empID){
           outFile<<"Employee total "<<empTotal<<"\n";
           std::cout<<"Employee total "<<empTotal<<"\n";
           depTotal+=empTotal;
           empTotal=0;
       }
      
      
       // if department changed , then print and reset
      
       if(prevDept!=dept){
           outFile<<"Department total "<<depTotal<<"\n";
           std::cout<<"Department total "<<depTotal<<"\n";
           tTotal+=depTotal;
           depTotal=0;
       }
      
      
       // add hours
       empTotal+=hours;
      
       outFile<<dept<<" ";
       outFile<<empID<<" ";
       outFile<<month<<"/";
       outFile<< std::setw(2) << std::setfill('0') <<day<<"/";
       outFile<<year<<" ";
       outFile<<hours<<"\n";
      
       std::cout<<dept<<" ";
       std::cout<<empID<<" ";
       std::cout<<month<<"/";
       std::cout<< std::setw(2) << std::setfill('0') <<day<<"/";
       std::cout<<year<<" ";
       std::cout<<hours<<"\n";
      
   }
  
   // for final , add the hours
   depTotal+=empTotal;
   tTotal+=depTotal;
  
  
   outFile<<"Employee total "<<empTotal<<"\n";
   outFile<<"Department total "<<depTotal<<"\n";
   outFile<<"Total hours worked "<<tTotal<<"\n";
   outFile<<count<<" records processed";
  
   std::cout<<"Employee total "<<empTotal<<"\n";
   std::cout<<"Department total "<<depTotal<<"\n";
   std::cout<<"Total hours worked "<<tTotal<<"\n";
   std::cout<<count<<" records processed";
  
  
   // close files
  
   inFile.close();
   outFile.close();
  
   return(0);
}

Add a comment
Know the answer?
Add Answer to:
C++ Need help with this code. Can't use "using namespace std;" Control break report Objectives Read...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • ***C++ Code*** What this Assignment Is About: -Learn to define and call void & non-void functions...

    ***C++ Code*** What this Assignment Is About: -Learn to define and call void & non-void functions -Learn to use reference variables Coding Guidelines for All Labs/Assignments (You will be graded on this) -Give identifiers semantic meaning and make them easy to read (examples numStudents, grossPay, etc). -Keep identifiers to a reasonably short length. -Use upper case for constants. Use title case (first letter is upper case) for classes. Use lower case with uppercase word separators for all other identifiers (variables,...

  • Need help with C++ assignment Assignment 1 and .txt files are provided at the bottom. PART...

    Need help with C++ assignment Assignment 1 and .txt files are provided at the bottom. PART A PART B Assignment 1 #include <iostream> #include <string> #include <fstream> #include <iomanip> #include <stdio.h> #include <ctype.h> #include <string.h> #include <algorithm> using namespace std; /** This structure is to store the date and it has three integer fields **/ struct Date{    int day;    int month;    int year; }; /** This structure is to store the size of the box and it...

  • ules for submission: 1. All problems must be solved using SQL code. The SQL code and...

    ules for submission: 1. All problems must be solved using SQL code. The SQL code and the output of your query should be cut and pasted into your MS Word file. a. Output Tables must be screenshot from DBMS b. Do not create a MS Word (or other application) table and paste the results into it. c. With the exception of Problems 4, 5 and 6, all output should be the result of a single step. Do not paste outputs...

  • //Need help ASAP in c++ please. Appreciate it! Thank you!! //This is the current code I have. #include <iostream> #include <sstream> #include <iomanip> using namespace std; cl...

    //Need help ASAP in c++ please. Appreciate it! Thank you!! //This is the current code I have. #include <iostream> #include <sstream> #include <iomanip> using namespace std; class googlePlayApp { private: string name; double rating; int numInstalls; int numReviews; double price;    public: googlePlayApp(string, double, int, int, double); ~ googlePlayApp(); googlePlayApp();    string getName(); double getRating(); int getNumInstalls(); int getNumReviews(); string getPrice();    void setName(string); void setRating(double); void setNumInstalls(int); void setNumReviews(int); void setPrice(double); }; googlePlayApp::googlePlayApp(string n, double r, int ni, int nr, double pr)...

  • I need help in C++ . Project 3 – Parking Deck Ticketing System Objectives: Use if,...

    I need help in C++ . Project 3 – Parking Deck Ticketing System Objectives: Use if, switch, and loop statements to solve a problem. Use input and output statements to model a real world application Incorporate functions to divide the program into smaller segments Instructions: Your task is to write a program that simulates a parking meter within the parking deck. The program will start by reading in the time a car arrives in the parking deck. It will then...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT