CS 1711 Lab - November 3 and 5, 1998
Counting, cases, and streams
Exercise: Switching off-by-one
A common error of loops, but less common for counted loops is the
`off-by-one'. You are going to look at a program's behaviour,
predict the off-by-one error in a while statement and fix it with
a counted loop. A switch statement also shows up.
- Obtain the file swby1.cpp from the usual directory.
Compile it and run it with input values of 1, 2, 3.
- What is wrong in the output?
- Modify the program so that it behaves correctly.
Store the result as swby2.cpp to show to your instructor.
- Now modify swby1.cpp leaving the loop fixed, but changing the
switch statement so that the program behaves correctly.
Store the result as swby3.cpp to show to your instructor.
In this exercise we are going to
implement a monthly cheque-book writing program. The program input is a sequence of
transaction records on the file trans.dat. The output is to be
written to the file cheque.out which you should store on your
M:-drive. An outline of the program is in
The first line of the data file is an integer code for the month, a number of
tranactions and an initial balance, for example 10 7 510.77 means the
month is October, there are 7 transactions from an initial balance of $510.77.
Each line following consists of a `D' or `C' (for debit or credit),
an integer (for the date), and a real value (for the amount).
The file cheque.out should look like (in part, for example):
Date Type Amount Balance
3 C 25.00 535.77
6 D 100.00 435.77
... ... ... ...
Final balance $455.44
where the last line simply gives the final monthly balance.
The steps are as follows:
- Obtain the file trans.dat, store it on your M: drive.
- Obtain the outline file cheque.cpp.
- Carefully read the function prototypes.
- Add any necessary file directives.
- Add suitable stub functions after the main function.
- Save your version with stubs as cheqstub.cpp.
- Write the required functions one at a time
to replace the stub functions.
Test each function after you have written it.
- Save your program as cheque.cpp. Demonstrate your
version with stubs and the finished program
(you should begin by showing the instructor
without a cheque.out file, then run the program...)