C++ Example
Our task is the following: we want to create a Unit Conversion Program that displays a menu of 5 options: 1 to convert degree Celcius to Fahrenheit, 2 to convert degree Celcius into Fahrenheit, 3 to convert meters into inches, and 4 to convert inches to meters. 0 is the final choice in the menu to exit the program. The user can pick an appropriate choice and the program will perform the necessary input, conversion, and output operation. The whole process should repeat until the user enters 0.
Stage 1: Basic Program Design | |
Stage 2: Basic Program in C++ | |
Stage 3: Adding Function Definitions | |
Stage 4: Fleshing out the Functions | |
Stage 5: The Finished Product |
First we want to get the very basic program design to work. That means we will not at all worry about the details but rather program the main function first. In fact, our first stage consists not of C++ code but rather of english statements about what we want to accomplish.
int main(void) { declare the necessary variables (choice, input, converted) loop: if a user presses anything put 0 then: - display the menu - ask the user to select a choice - if choice = 0 do nothing at all otherwise get the number to convert (input number) - if choice = convert degree F to C then convert the number, using the F to C conversion formula - if choice = convert degree C to F then convert the number, using the C to F conversion formula - if choice = convert degree meters to inches C then convert the number, using the M to inch conversion formula - if choice = convert degree inch to meters then convert the number, using the inch to m conversion formula display input number and converted number go back to the beginning of the loop unless choice was zero }
Now we rewrite the above basic design in the C++ language rather than english.
int main(void) { double input = 0.0; // to contain the number to convert double converted = 0.0; // to contain the converted number int choice = -1; // initialization: for while loop while (choice != 0) // testing: the test for the loop { showMenu(); // display the menu cin >> choice; // modification: user choice from keyboard if (choice != 0) // only do stuff if choice is not 0 { input = getInput(); if (choice == 1) converted = convertFtoC(input); else if (choice == 2) converted = convertCtoF(input); else if (choice == 3) converted = convertMtoInch(input); else if (choice == 4) converted = convertInchtoM(input); else cout << "\nInvalid input. Please try again ..."; cout << "\nYou converted: " << input << " to " << converted; } } }
From the above main program we see that we need several functions:
A function to display the menu. This function takes neither input nor output. | |
A function to obtain the number to be converted. No input, but it returns a double. | |
Several functions for the actual conversion process. Each takes one double as input and returns one double (the converted number). |
#include <iostream.h> void showMenu(); // shows the menu on the screen; no input or output double getInput(); // obtains a number from the keyboard; no input, double as output double convertFtoC(double); // convert from degrees F to degrees C; input and output are double double convertCtoF(double); // convert from degrees C to degrees F; input and output are double double convertMtoInch(double); // convert from meters to inches; input and output are double double convertInchtoM(double); // convert from inches to meters; input and output are double int main(void) { no change from before at all ! }
Note: At this stage the program should already compile if you did not make any typing mistakes.
Now that the above program compiled (and no sooner than that) we are ready to implement the various functions we need.
#include <iostream.h> // comments are deleted now for better readability void showMenu(); double getInput(); double convertFtoC(double); double convertCtoF(double); double convertMtoInch(double); double convertInchtoM(double); int main(void) { no change from before at all ! } void showMenu() { cout << "\n\nWelcome to the Unit Changer"; cout << "\n"; cout << "\n\t 1 \t to convert degrees F to degrees C"; ... // more statements similar to this cout << "\n\t 4 \t to convert Inch to Meter"; cout << "\n"; cout << "\n\t 0 \t to quit"; cout << "\n"; cout << "Your choice: "; } double getInput() { cout << "\nPlease enter number to be converted: "; double Tmp; cin>> Tmp; return Tmp; } double convertFtoC(double F) { return 5.0 / 9.0 * (F - 32); } // etc with the remaining functions
Well, this is a homework assignment. Therefore, I have not included the finished program here. However, based on the previous stages you should now be able to complete the assignment just fine.
As a quick reminder, don't forget to add the obligatory comments. If you like, you could also set the formatting to 'fixed with 2 digits precision', since that seems appropriate for our data. Don't forget to include the 'iomanip.h' file in this case.
Note: There is actually a small logical mistake in the layout of my main function. To see what I mean, try enter, say, '5' when asked for your choice. You would expect the program to say "invalid choice" .... does it ? For extra credit fix that error.
(bgw)