Lab 5. Multiple objects and function parameters.
Again I am providing skeleton source code for a
program that counts the number of characters
and lines typed by the user at the keyboard.
Complete the skeleton so that it reports the number of characters
excluding newline characters, and the number of lines.
For example, here is sample input and output:
% a.out
This is
some input
^D
Number of lines = 2
Number of characters = 17
%
YOU MAY NOT BE ABLE TO
FINISH THIS ASSIGNMENT IN THE LAB SESSION,
PLEASE COMPLETE IT IN YOUR OWN TIME. THE DUE DATE IS WEDNESDAY,
FEBRUARY 22ND., BY 5 PM
Follow these steps to complete the assignment:
- Download the source code by shift-clicking here.
- Use the editor to add methods to the Counter
class to:
- initialize the counter variable
- increment the counter
- print the value of the counter
- print a warning and terminate the program
when either the number of characters exceeds fifty or the number of
lines exceeds five.
- Compile and run the program several times to
test its results.
- Run the program once more with the same input
given above. Redirect the output to a file and insert a copy of the output
in the source code file.
- Print the file with the source code and
results and hand it to me (RTH).
- Send the source code only to the grader as
lab5 using the assignment submitter.
Points to note:
- You may get compilation errors and/or
warnings. Pay attention to these messages
because you program may not run correctly, or at all, because of
these errors.
- Don't forget to put your name in the file
comment block.
- Look at section 4.8 in the textbook for
related example programs.
- Termination of keyboard input is signaled by
typing a single ctrl-D on a separate
input line. You can also make the program read from a file by using
input indirection:
% a.out < input.file
- There is no need to put a ctrl-D in the file
since UNIX will effectively put one in
for you.
- The skeleton uses the member functions get and
eof for the object cin. You
should not need to alter this.
- You need two counter objects, one to count
lines and one to count characters. Each
one keeps its own count in a different copy of the counter variable.
- the input method get reads every character,
including spaces and newlines ('\n').
- Constructor functions have no return type.
- Terminate the program with a call to exit,
thus:
exit(1);
- You may want to customize the output function
(which prints the value of the counter)
by passing it a string. You can use the standard
string class to do this:
void print(string str) {
cout << str << ...
}
- You must include the header file to use the
string class:
#include <string>
(Note there is no .h on it)