namespace std;
void getInput();
void calculateAmount();
void printResult();
int main()
{
getInput();
calculateAmount();
printResult();
return 0;
}
void getInput()
{
cout << "Get input" << endl;
}
void calculateAmount()
{
cout << "Calculate amount" << endl;
}
void printResult()
{
cout << "Output result" << endl;
}</iostream></pre>
As
you can notice, we just broke the program into a collection of
functions. It still does more or less the same thing. Ok the next step
is to complete the getInput function. We need to decide on the
parameters and return types. We need to read the number of coffee cups.
Hence we have to send a reference parameter to the method. Change the
signature if the method to:
<pre>void getInput(int &noOfCups)</pre> and
implement the method like the following:
<pre>void getInput(int &noOfCups)
{
cout << "Enter the number of cups: ";
cin >> noOfCups;
}</pre> We got to change the main program to map this change. So replace the line
<pre>getInput();</pre> with <pre>int noOfCofeeCups = 0;
getInput(noOfCofeeCups);</pre>
Now we got to compile and run the program before moving
forward.
Next implement calculateAmount method: We need to define the constant
containing the price of a single coffee cup:
<pre>const float PRICE = 5.50;</pre>
now
change the signature of the calculateAmount method and implement it. We
need 2 parameters here, the number of cups and the price at which each
cup is sold.
<pre>float calculateAmount(int noOfCups, float price)
float calculateAmount(int noOfCups, float price)
{
return (noOfCups * price);
}</pre>
Remember to compile and run again. Finally we implement the printResult
method.
<pre>void printResult(int noOfCups, float price, float amount);
void printResult(int noOfCups, float price, float amount)
{
cout << noOfCups << " cups of cofee @ " << price << " each
totals to " << amount << endl;
}</pre>
Ok the final version of the program is:
<pre>#include <iostream>
using namespace std;
const float PRICE = 5.50;
void getInput(int &noOfCups);
float calculateAmount(int noOfCups, float price);
void printResult(int noOfCups, float price, float amount);
int main()
{
int noOfCofeeCups = 0;
getInput(noOfCofeeCups);
float result = calculateAmount(noOfCofeeCups, PRICE);
printResult(noOfCofeeCups, PRICE, result);
return 0;
}
void getInput(int &noOfCups)
{
cout << "Enter the number of cups: ";
cin >> noOfCups;
}
float calculateAmount(int noOfCups, float price)
{
return (noOfCups * price);
}
void printResult(int noOfCups, float price, float amount)
{
cout << noOfCups << " cups of cofee @ " << price << " each
totals to " << amount << endl;
}</iostream></pre>
We now have a working program that we are convinced is logically
correct and that solves the problem.The use of a
coding standard when
writing the code also made our lives easy.
We
would not have this much confidence in our program if we had used the
"big bang" approach. Incremental approach is more reliable, safe and
easy to follow. For students and beginners this would be an ideal way
to approach their programming problems with confidence.
About the Author
None