- Make a file called lab04.cpp from the
program skeleton
you see when you click here.
- Read the program. Pay special attention to the (header) comments
above each of the function definitions. The header comments explain
what each of the functions is supposed to do.
- In the main function, add the needed C++ instructions under each
of these comments:
- /* Call function PrintHowTo to print instructions */
- /* obtain a value for score1 by calling GetScore */
- /* if score1 is out of range print an error message */
- /* else obtain a value for score2 by calling GetScore */
- /* if score2 is out of range print an error message */
- /* else using function maximum print maximum score */
(Be careful to group the statements in the first else clause
with braces to form a compound statement. Remember that you have to
write this part of the program so that if the user enters an invalid
score the program will immediately print an error message and then stop
immediately.)
Use your understanding of what each function is supposed to do to guide
you as you write the function calls in this part of the program.
- In the next several steps we will put stub code
(placeholder code) into the sub-functions (the functions that
are called by the main function). After doing this the program will
still be unfinished. Nevertheless we will be able to compile and
execute the program. We will be able to check to see if the main
function is performing correctly.
Through the use of stubs, a programmer can build a program one function
at a time and test each function individually. This is a very valuable
technique. It keeps things pretty simple for the programmer, even when
the program being written is very large and complex.
- Replace the comment in the body of function GetScore
with the following stub code:
cout << "Entering function GetScore" << endl ;
return 300 ;
- Replace the comment in the body of function RangeError
with the following stub code:
cout << "Entering function RangeError" << endl ;
return false ;
- Replace the comment in the body of function maximum with
the following stub code:
cout << "Entering function maximum" << endl ;
return x ;
- Compile and execute the program. Debug the program before
continuing. The output should have a structure like this example:
This program interactively reads two quiz
scores and prints the maximum.
Entering function GetScore
Entering function RangeError
Entering function GetScore
Entering function RangeError
The maximum score is: Entering function maximum
300.
(In other words you should see two sets of outputs from the GetScore
and RangeError stubs, and then the output from your main
program that
reports the value returned by function maximum, with the stub
screen
output of function maximum inserted in between.)
After you have verified that the program produces output like this,
make a script called lab04_v1.script.
- Change the stub code in function GetScore so that it
returns 750 instead of 300. Change the stub code in function RangeError
so that it returns true instead of false.
Then compile and execute the program. Now the output should have a
structure like this example:
This program interactively reads two quiz
scores and prints the maximum.
Entering function GetScore
Entering function RangeError
You entered an invalid score.
Scores must be in the range 0-600 (inclusive).
Please try again.
(In other words, you should see exactly one output from GetScore,
one
from RangeError, and then the error message about invalid data
that you
put in your main program earlier. If you see more than one output from GetScore
and RangeError then you probably forgot to
enclose
between braces the statements in the main program after the first else.
If so, you need to fix that.)
When you have the program producing output like the example above, make
a script called lab04_v2.script.
- Change the stub code in function RangeError so that it
returns false (as before). Then delete the stub code
from inside GetScore and put code inside it that makes it do
what it is supposed to do.
The (header) comment above function GetScore says: "Function to
prompt for a score, read it, and return the score to the caller."
Therefore you write the code to make GetScore do exactly
that - no more or less.
The code in GetScore should declare a (local) int variable,
write a prompt for the user, input a number into the variable, and then
return the variable. If you want to see an example of the type of
function I mean, look at the function GetDim in this program.
Compile and execute the program (lab04.cpp). Debug the program before
continuing. If you run the program and enter scores of 250 and 340, the
output should have a structure like this example:
This program interactively reads two quiz
scores and prints the maximum.
Enter a quiz score: 250
Entering function RangeError
Enter a quiz score: 340
Entering function RangeError
The maximum score is: Entering function maximum
250.
- Replace the stub body of RangeError with code that makes
it work correctly (i.e. put code in the function that makes it do what
the header comment above RangeError says it should do - no more
and no less). You can do this with one line of code: a return
followed by a Boolean expression. You can also use if-(then)-else
logic.
- Similarly, replace the stub body of function maximum
with code that makes it do just what its header comment says it should
do. Try writing the code this way: declare a temporary variable. Use
if-(then)-else logic to test which variable is the max and store the
max in the temporary variable. Then return the value of the temporary
variable. Make sure that your function returns a correct result in all
cases: x less than y, x greater than y, or x equal to y.
- Compile and test the program by running it five times on
different kinds of inputs:
- The first score entered is out of range.
- The first score entered is in range, but the second is not.
- Both scores are in range and the first score is larger than
the second.
- Both scores are in range and the second score is larger than
the first.
- Both scores are in range and the scores are equal.
- Debug and re-test as necessary. When the program is working
correctly on all five types of input, make a single script that shows
all five tests, called lab04_final.script.
-
Upload:
- the final version of the program source code
- (filtered) scripts
- from step 8, called lab04_v1.script
- from step 9, called lab04_v2.script
- from step 14, called lab04_final.script
to the homework
submission system.