course.wilkes.edu/CS125Labs
Welcome to the site for CS 125 labs!

 

Lab 3b: Controlling Function Behavior

Prev | Next | Lab 3b

Introduction

Today's lab explores some C++ statements that we can use to write more sophisticated functions. The exercise consists of two parts: In the first part, we examine a problem whose solution requires more complicated behaviors than we have seen thus far, and introduce the new statements to elicit that behavior. In the second part, we introduce a new program-development tool called the debugger and use it to study the behavior of the new statements.

Part I: The Payroll Problem

Our problem today is to write an interactive payroll program, that a small-business owner might use to simplify computing the payroll at his or her business. For the sake of simplicity, we will assume that all employees at the business are hourly employees.

Recall that object-centered design involves several stages:

  1. Identify the behavior we wish the program to exhibit in order to solve the problem.
  2. Identify the objects in that behavior.
  3. Identify the operations in that behavior.
  4. Organize the operations and objects into an algorithm.
Now that we know about functions and function libraries, we must incorporate them into our design scheme. Functions can be thought of as the means of creating our own operations, so the natural place to incorporate them is in step 3:
  1. Identify the behavior we wish the program to exhibit in order to solve the problem.
  2. Identify the objects in that behavior.
  3. Identify the operations in that behavior.
    If there is no predefined way to perform an operation,
      build a function to perform it.
    If a function is reuseable, store it in a library.
  4. Organize the operations and objects into an algorithm.

Once we have our design, we can encode it in a programming language, test and debug the resulting program, and then perform any maintenance required over its lifetime.

Today's exercise is to use these stages to develop a program that solves our payroll problem.

Preparing Your Workspace

Begin by creating a directory for this exercise (e.g., labs/3b) and then make it your working directory. Then save copies of the files Makefile 3b,payroll.cpp,pay.h,pay.cpp, and pay.doc. in that directory. Verify that the files are in your new directory, and then use your text editor to open payroll.cpp, and take a moment to personalize its opening documentation.

Design

As always, spending a bit of time planning how to attack our problem will result in a better solution. To do so, we follow the steps of object-centered design.

Behavior. We can begin by visualizing and writing down how we want our program to behave. One approach is to have our program behave something like the following:

   This program computes the payroll interactively.

To begin, enter the number of employees: 3

Enter the name, hours and rate for employee 1: Joe 25 5.25 Joe 131.25

Enter the name, hours and rate for employee 2: Mary 35 6.15 Mary 215.25

Enter the name, hours and rate for employee 3: Sue 45 6.15 Sue 292.125

Put into words, our program should

   display on the screen a greeting, followed by a prompt for the 
   number of employees, which it should then read from the keyboard.  
   For each employee, our program should then display a prompt for 
   their name, hours and rate of pay.  The program should then
   read these values from the keyboard, and compute and display
   the employee's pay, along with their name.

Objects. If we identify the nouns in this behavioral description, we get the following list:

Description Type Kind Name
The screen ostream varying cout
A greeting string constant --
A prompt for input string constant --
The number of employees int varying numberOfEmployees
The keyboard istream varying cin
An employee's name string varying name
An employee's hours of work double varying hours
An employee's rate of pay double varying rate
An employee's pay double varying pay

From this list, we can build a precise specification of how our program is to behave:

   Input:       The number of employees;
                Each employee's name, hours of work, and rate of pay.
   Output:      Each employee's name and pay.
Use this information to complete the specification of payroll.cpp.

Operations. If we identify the operations in our behavioral description, we get this list:
Description Predefined? Name Library?
display a string (greeting, prompts, labels) yes << iostream
Read an int (numberOfEmployees) from the keyboard yes >> iostream
read a string (name) from the keyboard yes >> iostream
read a double (hours, rate) from the keyboard yes >> iostream
compute an employee's pay, given their hours and rate from the keyboard no ?? ??
display a double (pay) on the screen yes << iostream
repeat operations 3-6 once for each employee yes for --

As indicated, most of these operations are provided for us by C++. Operations 1-4 and 6 should be familiar by now. There is no predefined C++ capability to perform operation 5, and since accounting for overtime pay makes it non-trivial, we will write a function to perform this operation. Since it seems like an operation that might be useful again some day, we will store it in a library. Finally, operation 7 involves a new C++ statement that we haven't seen before called the for statement, which provides a convenient way to repeat a group of statements a predetermined number of times.

Algorithm. Even without knowing the details of how we will compute the pay, we can organize our operations into an algorithm for our problem:

  1. Via cout, display a greeting on the screen, plus a prompt for the number of employees.
  2. From cin, read an integer, storing it in numberOfEmployees.
  3. For each value empNum in the range 1 to numberOfEmployees:
    1. Via cout, display a prompt for the name, hours and rate of employee empNum.
    2. From cin, read a string, a double and a double, storing them in name, hours and rate.
    3. Compute pay, using hours and rate.
    4. Via cout, display name and pay.
    End loop.

Coding, Testing and Debugging

Once we have designed an algorithm to solve our problem, we must encode that algorithm in the a high level programming language (i.e., C++). We can begin this process by constructing a minimal C++ program, consisting of

   int main()
   true
(These lines are already present in payroll.cpp, after its opening documentation.) To make sure that this much is correct before we add to it, take a moment to translate payroll.cpp. To facilitate the translation, we have provided a complete Makefile, so use the compile command and make to perform the translation:

   Compile command: make -k
If you examine the Makefile, you will note that each call to g++ uses the -g switch, which tells g++ to save the information needed by the GNU debugger (discussed below). This switch must be used to compile and link in order for the debugger to be used.

Given the minimal C++ program, we are ready to encode our algorithm using stepwise translation. We therefore begin with the first step:

1. Via cout, display a greeting on the screen,
  plus a prompt for the number of employees.

Coding: This step can be performed using a C++ output statement, which we have seen before:

   cout << Value1 << Value2 << ... << ValueN;
so add an output statement to payroll.cpp that displays the following message:

   This program computes the payroll interactively.

To begin, enter the number of employees:

Don't forget the #include <iostream> and using namespace std; directives!

Before proceeding to the next step, check the correctness of what you just wrote by retranslating your program. The compiler will alert you to any syntax errors in your statement. If an error is listed, you can infer that the error(s) lies in the text ' you just added, since the program was error-free before that. Find your error(s) within those lines and use the editor to correct them.

When your source program compiles correctly, execute payroll to test that it displays the intended message. If not, the statements you have added contain logic errors. (i.e., the statements you have added are syntactically correct, but they don't accomplish their task correctly.) Compare your program's statements against the output produced by payroll and modify them as needed. When your program is error-free, proceed to the next step of our algorithm.

2. From cin, read an integer, storing it in numberOfEmployees>.

Coding: We can encode this step in C++ using an input statement:

   cin >> Var1 >> Var2 >> ... >> VarN; 
Add an input statement to your source program to perform step 1. Don't forget to declare a variable to store numberOfEmployees! Check that what you have added is free of syntax errors before continuing.

Note that we could use an assert() at this point to check that numberOfEmployees is non-negative. However, doing so is unnecessary, thanks to our next step.

3. For each value empNum in the range 1 to numberOfEmployees:
  a. Via cout, display a prompt for the name, hours and rate
      of employee empNum.
  b. From cin, read a string, a double and a double,
      storing them in name, hours and rate.
  c. Compute pay, using hours and rate.
  d. Via cout, display name and pay.
End loop.

Coding: Let's take this step a piece at a time, starting with the outer part (3) and then doing the inner parts (a-d).

3. For each value empNum in the range 1 to numberOfEmployees:
  ...
End loop.

The purpose of step 3 is to count from 1 to the number of employees, and repeat steps a[not equal]d that many times. That is, if there are three employees, then steps a[not equal]d should be repeated three times.

For situations like this that require repetitious behavior, C++ supplies the for statement. The for statement can use its own local variable, called a loop-control variable, to do the counting. A simplified general form of a for statement that counts from firstValue to lastValue is:

   for (Type loopVar = firstValue; loopVar <= lastValue; loopVar++)
   {
      Statements
   }
where loopVar is the loop-control variable, and the Statements between the curley-braces are called the body of the loop. The behavior of this statement is as follows:
  1. loopVar is declared and initialized to firstValue.
  2. loopVar is compared against lastValue.
  3. If the comparison evaluates to true:
    1. Statements get executed.
    2. loopVar++ is executed.
    3. Go to 2.
    Otherwise, control proceeds to the next statement.

In our problem, we must count from 1 to the value stored in numberOfEmployees, using empNum as the name of the loop-control variable. To do so, we can write:

   for (int empNum = 1; empNum <= numberOfEmployees; empNum++)
   true
leaving its Statements empty for the moment.

Note that if the user enters a negative value for numberOfEmployees, the body of the loop will not be executed, because the loop's body is only executed if the condition controlling the loop evaluates to true, and a negative value for numberOfEmployees will make this condition false.

Add this to payroll.cpp and then check its syntax. When the compiler generates no errors, proceed to the inner steps.

3a. Via cout, display a prompt for the name, hours and rate
  of employee empNum.
This is a normal output statement, like those we have seen before, except that in addition to displaying a string, it displays the value of our loop-control variable to generate an "employee number." That is, if numberOfEmployees is 3, then our loop will execute three times, so add an output statement to the body of the loop that will generate the following:


   Enter the name, hours and rate for employee 1:
   Enter the name, hours and rate for employee 2:
   Enter the name, hours and rate for employee 3:

Then check the syntax of what you have written using the compiler. When your program translates correctly, run it and enter 3 for the number of employees, to verify that what you have written is free of logic errors.

3b. From cin, read a string, a double and a double,
  storing them in name, hours and rate.

This step can be encoded by adding a normal input statement to the body of the loop, after the output statement, so take a moment to do so.

However, before such a statement will compile correctly, the objects name, hours and rate must be declared. This raises an interesting question: Where should these objects be declared?

If we declare name, hours and rate within the body of the loop, then they will be redeclared anew each execution of the body, wasting time. For the sake of efficiency, they should instead be declared immediately before the for statement. That way, they will still be declared near their first use, but time will not be wasted reprocessinging their declarations every time the loop body executes.

Add the necessary statements to your program to (i) declare name, hours and rate(outside the loop), and (ii) fill these objects with values entered from the keyboard (inside the loop). To declare name as a string, you will need to include the string system file:

   #include <string>
(The string header file does not end in .h.) Then check your code's syntax using the compiler, and continue when it is correct.

Anytime we program an input operation, we should consider the possibility of human error: what could the user do to foul up our program? There are several possibilities here:

  1. The user could enter a negative value for hours.
  2. The user could enter a value for hours greater than 168 (the number of hours in a week).
  3. The user could enter a negative value for rate.
The user could also enter a value for rate greater than the maximum pay rate; however our problem does not specify a maximum pay rate, so we will ignore that potential source of error for the moment.

Since any of these three errors will cause our program to generate incorrect pay amounts, our code should safeguard against them. To do so, add an assert() to the program that only allows the program to continue if hours is non-negative, and hours is 168 or less, and rate is non-negative. Use a single assert() call to do so. (Don't forget the #include directive!)

Then check the syntax of what you have written using the compiler, then run your program and test that your assert() halts the program if any of these three errors occur. When it correctly "catches" these errors, continue.

3c. Compute pay, using hours and rate.

This operation is not predefined, and so we will design and build a function to perform it.

Function Analysis. To compute pay for an arbitrary employee, our function needs their hours and rate. Since these values will differ from employee to employee, our function should receive these values from its caller.

Function Behavior. Our function should receive the hours and rate from its caller. If hours is less than or equal to the number of hours in a work week (40 in the U.S.), then our function should compute the total pay as hours times rate. Otherwise, it should compute the normal pay as the work week times the rate; compute the overtime pay as the overtime hours times the rate; and then compute the total pay as the sum of the overtime pay and the normal pay.

Function Objects. Ignoring nouns like "our function" and "caller", we can identify the following objects in this description:

Description Type Kind Movement Name
hours of work double varying received hours
pay rate ($$/hour) double varying received pay
hours in a work week double constant local NORMAL_WEEK
total pay double varying returned totalPay
normal pay double varying local normalPay
overtime pay double varying local overtimePay
overtime hours double varying local hours - NORMAL_WEEK
overtime pay factor double constant local OVERTIME_FACTOR

For each received object, we must provide a parameter to store that value. We can thus specify the behavior of our function as follows:

   Receive: hours and rate, both doubles.
   Precondition: 0 <= hours and hours <= 168 and 0 <= rate.
   Return: the total pay, a double.
These observations allow us to create the following stub for our function:
   double ComputePay(double hours, double rate)
   true
Each object that is neither received nor returned must be defined as a local object, within the body of our function. For example, OVERTIME_FACTOR will be a local constant double object, defined to have the value 1.5; while normalPay will be a local variable double object.

Since this seems like a function that might be reuseable some day, we will store its definition in a library named pay. Open pay.cpp, and write this stub there. Since other programs besides payroll.cpp may be calling this function (and not checking the precondition ahead of time), make the first line in the function an assert() call that checks the function's precondition. (Copy-and-paste your assert() from payroll.cpp.)

Before this stub, add an include directive:

   #include pay.h
to insert the header file of our pay library into the implementation file when it is compiled. Then add a prototype of this function to the header file pay.h and the documentation file pay.doc. Finally, copy-and-paste the function's specification into pay.doc.

Function Operations. From our behavioral description, we have these operations:

Description Defined? Name Library?
1 receive hours and rate from caller yes function call
mechanism
built-in
2 compare hours and NORMAL_WEEK</TM>
in less-than-or-equal relationship
yes <= built-in
3 compute totalPay normally
(hours times rate)
yes * built-in
4 compute totalPay for overtime
4a compute normalPay
(NORMAL_WEEK times rate)
yes * built-in
4b compute overtimePay
(hours minus NORMAL_WEEK times rate times OVERTIME_FACTOR)
yes -, *, * built-in
4c compute totalPay
(normalPay plus overtimePay)
yes + built-in
5 if (2) is true, perform (3), otherwise perform (4) yes if built-in
6 return totalPay yes return built-in

We have seen each of these operations before, except for operation (5), which introduces a new behavior in which we must select one group of statements or another, but not both. As we shall see shortly, the C++ if statement provides this behavior.

next...

Prev | Next | Lab 3b

 
  Home

Help

Lab 0

Lab 1

Lab 2

Lab 3a

Lab 3b

Lab 4

Lab 5

Lab 6

Lab 7

Lab 8

Lab 9

Lab 10

Lab 11

Lab 12

Lab 13



Membership

Login




Last update: Wednesday, August 23, 2000 at 10:26:37 AM.
visitors to this page.