Accumulating Totals in Single-Level Control Break Programs Summary In this lab,
ID: 3853593 • Letter: A
Question
Accumulating Totals in Single-Level Control Break Programs
Summary
In this lab, you will use what you have learned about accumulating totals in a single-level control break program to complete a C++ program. The program should produce a report for a supermarket manager to help her keep track of hours worked by her part-time employees. The report should include the day of the week and the number of hours worked for each employee for each day of the week and the total hours for the day of the week.
The student file provided for this lab includes the necessary variable declarations and input and output statements. You need to implement the code that recognizes when a control break should occur. You also need to complete the control break code. Be sure to accumulate the daily totals for all days in the week. Comments in the code tell you where to write your code.
Instructions
Study the prewritten code to understand what has already been done.
Write the control break code, including the code for the dayChange() function, in the main() function.
Execute the program by clicking the "Run Code" button at the bottom of the screen. Use the following input values:
Monday—6 hours (employee 1)
Tuesday—2 hours (employee 1), 3 hours (employee 2)
Wednesday—5 hours (employee 1), 3 hours (employee 2)
Thursday—6 hours (employee 1)
Friday—3 hours (employee 1), 5 hours (employee 2)
Saturday—7 hours (employee 1), 7 hours (employee 2), 7 hours (employee 3)
Sunday—0 hours
[And here is the pre-written code:]
// SuperMarket.cpp - This program creates a report that lists weekly hours worked by employees of a supermarket. The report lists total hours for each day of one week.
// Input: Interactive
// Output: Report.
#include <iostream>
#include <string>
using namespace std;
int main()
{
// Declare variables.
const string HEAD1 = "WEEKLY HOURS WORKED";
const string DAY_FOOTER = " Day Total ";
// Leading spaces in DAY_FOOTER are intentional.
const string SENTINEL = "done"; // Named constant for sentinel value.
double hoursWorked = 0; // Current record hours.
string dayOfWeek; // Current record day of week.
double hoursTotal = 0; // Hours total for a day.
string prevDay = ""; // Previous day of week.
bool notDone = true; // loop control
// Print two blank lines.
cout << endl << endl;
// Print heading.
cout << " " << HEAD1 << endl;
// Print two blank lines.
cout << endl << endl;
// Read first record
cout << "Enter day of week or done to quit: ";
cin >> dayOfWeek;
if(dayOfWeek == SENTINEL)
notDone = false;
else
{
cout << "Enter hours worked: ";
cin >> hoursWorked;
prevDay = dayOfWeek;
}
while(notDone == true)
{
// Implement control break logic here
// Include work done in the dayChange() function
}
cout << " " << DAY_FOOTER << hoursTotal << endl;
return 0;
} // End of main()
Explanation / Answer
Code:
#include <iostream>
#include <string>
using namespace std;
int totalHours[7];
int empHours[3][7];
char Day[7][10]={"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
int findDay(string &dayOfWeek)
{
if(dayOfWeek=="Monday")
return 0;
else if(dayOfWeek=="Tuesday")
return 1;
else if(dayOfWeek=="Wednesday")
return 2;
else if(dayOfWeek=="Thursday")
return 3;
else if(dayOfWeek=="Friday")
return 4;
else if(dayOfWeek=="Saturday")
return 5;
else
return 6;
}
bool dayChange(string &dayOfWeek, string &prevDay)
{
return dayOfWeek!=prevDay;
}
int main()
{
// Declare variables.
const string HEAD1 = "WEEKLY HOURS WORKED";
const string DAY_FOOTER = " Day Total ";
// Leading spaces in DAY_FOOTER are intentional.
const string SENTINEL = "done"; // Named constant for sentinel value.
double hoursWorked = 0; // Current record hours.
string dayOfWeek; // Current record day of week.
double hoursTotal = 0; // Hours total for a day.
string prevDay = ""; // Previous day of week.
bool notDone = true; // loop control
int empId,day;
// Read first record
cout << "Enter day of week or done to quit: ";
cin >> dayOfWeek;
if(dayOfWeek == SENTINEL)
notDone = false;
else
{
cout << "Enter empId, hours worked: ";
cin >> empId>> hoursWorked;
prevDay = dayOfWeek;
}
day=findDay(dayOfWeek);
while(notDone == true)
{
totalHours[day]+=hoursWorked;
empHours[day][empId-1]+= hoursWorked;
prevDay = dayOfWeek;
cout<<"Enter day of week or done to quit:";
cin>>dayOfWeek;
if(dayOfWeek == "done")
break;
cout << "Enter empId, hours worked: ";
cin >> empId>> hoursWorked;
bool dayChg=dayChange(dayOfWeek,prevDay);
if(dayChg)
day=findDay(dayOfWeek);
}
// Print two blank lines.
cout << endl << endl;
// Print heading.
cout << " " << HEAD1 << endl;
// Print two blank lines.
cout << endl << endl;
for(int i=0;i<7;i++){
cout<<Day[i]<<":";
for(int j=0;j<3;j++)
cout<<"Emp"<<j+1<<" hours:"<<empHours[j][i]<<",";
hoursTotal=totalHours[i];
cout << " " << DAY_FOOTER << hoursTotal << endl;
}
return 0;
} // End of main()
Output:
Enter day of week or done to quit: Monday
Enter empId, hours worked: 1 6
Enter day of week or done to quit:Tuesday
Enter empId, hours worked: 1 2
Enter day of week or done to quit:Tuesday
Enter empId, hours worked: 2 3
Enter day of week or done to quit:Wednesday
Enter empId, hours worked: 1 5
Enter day of week or done to quit:Wednesday
Enter empId, hours worked: 2 3
Enter day of week or done to quit:Thursday
Enter empId, hours worked: 1 6
Enter day of week or done to quit:Friday
Enter empId, hours worked: 1 3
Enter day of week or done to quit:Friday
Enter empId, hours worked: 2 5
Enter day of week or done to quit:Saturday
Enter empId, hours worked: 1 7
Enter day of week or done to quit:Saturday
Enter empId, hours worked: 2 7
Enter day of week or done to quit:Saturday
Enter empId, hours worked: 3 7
Enter day of week or done to quit:done
WEEKLY HOURS WORKED
Monday:Emp1 hours:6,Emp2 hours:2,Emp3 hours:5, Day Total 6
Tuesday:Emp1 hours:0,Emp2 hours:3,Emp3 hours:3, Day Total 5
Wednesday:Emp1 hours:0,Emp2 hours:0,Emp3 hours:0, Day Total 8
Thursday:Emp1 hours:0,Emp2 hours:0,Emp3 hours:0, Day Total 6
Friday:Emp1 hours:0,Emp2 hours:0,Emp3 hours:0, Day Total 8
Saturday:Emp1 hours:0,Emp2 hours:0,Emp3 hours:0, Day Total 21
Sunday:Emp1 hours:0,Emp2 hours:0,Emp3 hours:0, Day Total 0