( Latest Revision: Oct 25, 2014 )

Directions for Lab #5

In this lab we will work on if-(then)-else logic, techniques of top-down program design, and input checking.

Read these directions all the way through at least one time before trying to perform any of the steps.

We will work on a program whose purpose is first to read two quiz scores entered from the keyboard and then to print their maximum.

However, if the user enters an invalid (out-of-range) quiz score, we want the program to print an error message and then just stop (without asking for the second input, or trying to calculate a maximum, or anything else. We need to put if-(then)-else logic into the program to make it work this way.

It would be possible to design the program to read both quiz scores and then check to see if they are both valid. However, in this lab you must write the program so that, if the first score entered by the user is not valid, the program will print an error message and stop without reading or prompting for a second score.

There is a structure diagram of the assigned program here. It shows that the main program calls four level-two functions: PrintInfo, GetScore, RangeError, and maximum.

Individual steps:

  1. Make a file called lab05.cpp from the program skeleton you see when you click here.

  2. Read the program. Pay special attention to the comments under the function declarations near the top of the program file. The comments explain what the functions are supposed to do.

  3. In the main function, add the needed C++ instructions under each of these comments:


    Be careful to group the statements in the first else clause with braces to form a compound statement (aka a block). The first else clause is the outer else clause. It has an if-else statement nested inside it. Remember that you have to write the logic of the program so that any time the user enters an invalid score, the program will immediately print an error message and stop.

    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. (Important: Make sure to first read and understand all the comments under the function declarations (aka prototypes). The functions must do exactly what the comments say the functions do -- nothing less, and nothing more. Be guided by this when you write the part of the program that uses the functions.)

    After you complete this step, raise your hand and ask me to check your work.

  4. In the next several steps you will put stub code (placeholder code) into the level-two functions (the functions that are called by the main function). After you do this, the program will still be unfinished. However, you will be able to compile and execute the program. You will be able to check to see if main is performing correctly (the main function of the program).

    Through the use of stubs, a programmer can build and test a program one function at a time. This is a very valuable technique. It keeps things pretty simple for the programmer, even when the program is very large and complex.

  5. Under the comment in the body of function GetScore write the following stub code:
         cout << "Entering function GetScore" << endl ;
         return 300 ; 
  6. Under the comment in the body of function RangeError write the following stub code:
         cout << "Entering function RangeError" << endl ;
         return false ; 
    The return value of false means that the score (300) is OK - it is within the correct range.

  7. Under the comment in the body of function maximum write the following stub code:
         cout << "Entering function maximum" << endl ;
         return x ; 
  8. 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 of the two scores is: Entering function maximum
    300
    
    In other words, you should first see two sets of outputs from the stub GetScore and stub RangeError functions, and then output from the stub maximum function. The output from the stub maximum function will probably be sandwiched between output from a cout in main, the cout that tells what the maximum is.

    After you have verified that the program produces output like this, send me a copy of the source code by e-mail. Use the subject line: CS1500_Lab5_Vers1

  9. Change the stub code in function GetScore so that it returns 950 instead of 300. Change the stub code in function RangeError so that it returns true instead of false, which means that the score (950) is NOT within the correct range. 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-800 (inclusive).
    Please try again.
    (In other words, you should see exactly one output from GetScore, one from RangeError, and then an error message about invalid data. If you see more than one output from GetScore and RangeError then probably either you forgot to put the first else keyword in your program, or you forgot to enclose between braces the statements after the first else. Whatever the error(s), figure out what's wrong, and fix it.)

    When you have the program producing output like the example above, send me a copy of the source code by e-mail. Use the subject line: CS1500_Lab5_Vers2

  10. 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, as a finished product.

    The comment below the declaration of function GetScore says:
    "Prompt for a score (an int), read it & return it."
    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 local 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 (lab05.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
    Entering function maximum
    The maximum of the two scores is: Entering function maximum
    250
    
  11. Replace the stub body of RangeError with code that makes it work correctly (i.e. replace its stub code with code that makes RangeError do what the comment below the declaration of 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 that selects between two different return statements.

  12. Similarly, replace the stub body of function maximum with code that makes it do just what the comment below its declaration says it should do. (Note: function maximum is not supposed to write to the screen.) Try writing the code this way: first declare a temporary variable; second, use if-(then)-else logic to do both these things: test which parameter is the max, and store the max in the temporary variable; finally, 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.

  13. Compile and test the program by running it five times on different kinds of inputs:


  14. 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. E-mail me a copy of the final version of the program source code with subject line: CS1500_Lab5_Fin and e-mail me a copy of the (filtered) script with subject line: CS1500_Lab5_Scr

    As usual, you can refer to the Hello World! example for details on how to make and filter a script file, and on how to e-mail files to me.