- For lab1 we're writing a short program to compute a (10pin) bowler's league
handicap based on the scores from their last three games. We'll get them to provide
their name and the three scores, then we'll compute their average and handicap.
Our program will then display a summary of their information back to them.
The steps below will walk through the construction of the program
- We've got the basic structure of a C++ program in our lab1.cpp already,
#include <iostream>
int main()
{
std::cout << "Welcome to lab1!" << std::endl;
return 0;
}
|
- First we'll add another library we're going to need: string (we'll
use a string variable to hold their name eventually).
With the new libary, the top of the program will now look like:
#include <iostream>
#include <string>
Change the file content, save, then try recompiling in the terminal window
make lab1x
If you get any compilation errors then go back to the editor and keep fixing/compiling
until it's ok, then try running it. (For the moment, the behaviour will be exactly the same as before.)
./lab1x
- Next we'll display a short intro to the program so the user has some idea of
what's going on.
We'll add this using more cout statements, replacing the single cout we originally
had, e.g.
std::cout << "Welcome to BCapper: the program that computes your bowling handicap";
std::cout << std::endl;
std::cout << "You will be asked to enter your name and the scores of your last three ";
std::cout << "ten-pin bowling scores (0-300)." << std::endl;
std::cout << "BCapper will then tell your your league handicap." << std::endl;
std::cout << std::endl;
Again try compiling/fixing until it compiles cleanly, and now when we run it we should
see the message above.
- Now we can add some variables to hold the bowler's name, the three game
scores, and the computed average and handicap.
The name will be entered and stored as a single word, so we'll use a string variable
to hold that.
The three game scores can be stored as integers, while the average and handicap
can be stored as floats.
For each variable we'll pick a meaningful name and we'll put a descriptive comment
beside the variable.
We'll put all this just after the { at the start of the main routine, e.g.
int main()
{
std::string name; // the bowler's name
int game1, game2, game3; // the bowler's past three scores
float average, handicap; // computed based on the three scores
... the cout's from the previous step would be down here ...
Again try compiling/fixing until it compiles cleanly other than warnings about unused variables
(those will go away as we actually get the user to input some data).
If we run it now (i.e. the
./lab1x command) it should still just give the descriptive message then end.
- Now, one-by-one, we'll prompt the user to enter part of the information
and we'll read it into a variable, e.g.
std::cout << "Enter the your name (one word only): ";
std::cin >> name;
These will go below our earlier cout (so they see the program description *then* it starts
asking for data) but above the return 0 (since that effectively ends a run of the main routine).
Do similarly for each of the game score variables (game1, game2, game3)
after giving the user a clear description of what they should be entering.
As usual, compile and fix until it compiles ok. (Notice the warnings for
the variables have changed, now it tells us we've set values for them but
aren't using them. This will be fixed in the next step.)
Now if we run the program it
should prompt us to enter the various values, stopping and waiting for us to
enter a value each time.
- Next we'll do the calculation of the average and handicap.
For the average, it is simply the sum of the three games divided by 3.0, e.g.
average = (game1 + game2 + game3) / 3.0;
For the handicap, a typical league calculation might be 90% of the difference
between the bowler's average and a designated league base score such as 200:
handicap = (200 - average) * 0.9;
However, in accordance with our coding standards, we'll designate named constants
to use in place of the 'hard-coded' 200 and 0.9, e.g.
const int BaseScore = 200; // expected league-average score, used for handicap calculation
const float HCadj = 0.9; // league-designated adjustment (generally 0.8 to 1.0)
Our calculation of handicap would then become
handicap = HCadj * (BaseScore - average);
- Finally we'll add a section to display all the stored data back to the user
in a meaningful way. This will go after (below) the completed calculations
(but before/above the return 0).
std::cout << "Based on your average score (" << average << "), ";
std::cout << "your league handicap is " << handicap << std::endl;
std::cout << std::endl;
Compile and fix (as usual), and hopefully the program should behave as expected when run:
giving the user a description of the program, prompting the user to enter their data,
doing the calculations, then displaying the final results.
Keep tweaking and fixing the program until you're satisfied it is working correctly
and you're happy with the appearance of the output.