SOURCE FILE: 200_floorSpace03.cpp
/*
Program to display the floor space for a room
Fleshed-out design.
* Now there are declarations of the variables
used in the main function
* Now there are prototypes for the functions
+ prototypes tell
- what data type, if any the function returns
- the name of the function
- the number and types of parameters required by the function
* Now there are also definitions of the functions
* Although the program is not finished, there are no syntax
errors. So we can compile and execute this program.
That's an important idea. We want to be able to execute
this program so we can verify that the main function is
correct.
+ Except for the main function, the function definitions in
this program are NOT FINISHED. They contain STUB CODE that
is only supposed to be here temporarily.
+ the STUB CODE is place-holder code that will help us with
the testing of the main function.
* A good way to write a big program is to write it one small
function at a time, and to test the program after finishing
each function. Generally the function we test needs to
call other functions. Usually we provide STUB FUNCTIONS
for the test.
* As indicated above, we can compile and execute this program.
Doing that will give us output to the screen that shows that
the main function operates correctly. When the program
executes, its output will show that the main function performs
all the actions it is supposed to perform, and that it
performs them in the correct order. Both are important -
doing all the actions required, and getting the order (flow of
control) correct. Some of the actions the main function
performs are FUNCTION CALLS, and some are just ordinary
instructions, like the if-else statement and the cout
statements.
*/
#include <iostream>
using namespace std ;
/* ================================== */
/* FUNCTION DECLARATIONS (PROTOTYPES) */
/* ================================== */
void directions(void);
/* Print directions for using the program. */
int GetDim(void);
/* Prompt the user for one dimension of a floor and
return that dimension */
int area(int, int);
/* Return the floor space (area of the floor)
given two dimensions (length and width). */
/* ================================== */
/* ================================== */
int main(void)
{
int dim1, // one dimension of the room
dim2, // another dimension of the room
FloorSpace; // area of floor in room
/* Step One: Print Directions for the user */
directions() ;
/* Step Two: Get the dimensions of the floor from the user */
dim1 = GetDim();
dim2 = GetDim();
/* Step Three: IF either dimension is not positive
print an error message
ELSE Compute FloorSpace as product and
print the answer out for the user to see
*/
if ( (dim1 <= 0) || (dim2 <= 0) ) // invalid data
{
cout << "\nSorry, only positive dimensions are allowed.\n";
cout << "Please reexecute the program.\n\n";
}
else // valid data
{
FloorSpace = area(dim1, dim2);
cout << "\nThe floor space is " << FloorSpace << " square feet.\n\n";
/* STUDY QUESTION: Can we reduce the code above to one statement
by putting the call to the area function in the output statement? */
}
return 0;
}
/* ================================== */
/* FUNCTION DEFINITIONS */
/* ================================== */
/* ================================== */
/* ALL DEFINITIONS BELOW ARE STUBS */
/* ================================== */
/* ================================== */
void directions(void)
{
// stub output so we will know when the function runs
cout << "Function 'directions()' executed.\n" ;
}
/* ================================== */
/* ================================== */
int GetDim(void)
{
// stub output so we will know when the function runs
cout << "Function 'GetDim()' executed.\n" ;
/* return SOMETHING, so that the caller gets a value
back from the function. */
return 39 ; // arbitrary return value
}
/* ================================== */
/* ================================== */
int area(int dim1, int dim2)
{
// stub output so we will know when the function runs
cout << "Entering function 'area()' ... \n" ;
/* stub output tells what the values of the
parameters are, so we know that the function
received the correct thing from the caller */
cout << "dim1 is: " << dim1 << endl ;
cout << "dim2 is: " << dim2 << endl ;
cout << "Returning from function 'area()' ... \n" ;
/* return SOMETHING, so that the caller gets a value
back from the function. */
return 42 ; // arbitrary return value
}
/* ================================== */