SOURCE FILE: EX0407.CPP
// Example 4.7. Program to display the floor space for a room
#include <iostream.h>
int main(void)
{
void directions(void); // prototypes
int GetDim(void);
int area(int, int);
int dim1, // one dimension of the room
dim2, // another dimension of the room
FloorSpace; // area of floor in room
directions();
dim1 = GetDim();
dim2 = GetDim();
// only process positive floor dimensions
if ( (dim1 <= 0) || (dim2 <= 0) ) // invalid data
{
// user types a nonpositive value for dim1 or dim2
cout << "\nSorry, only positive dimensions are allowed.\n";
cout << "Please reexecute the program.\n";
}
else // valid data
{
FloorSpace = area(dim1, dim2);
cout << "\nThe floor space is "
<< FloorSpace << " square feet.\n";
}
return 0;
}
//
// Function to print program directions
// Pre: none
// Post: Program directions have been printed.
//
void directions(void)
{
cout << "This program will give you the floor space ";
cout << "in square feet\n";
cout << "for a room of your choosing.\n\n";
}
//
// Function to prompt the user for one dimension
// of a floor and to return that dimension
// Pre: The user enters an integer dimension.
// Post: The function has returned that dimension.
//
int GetDim(void)
{
int dimension; // one dimension of floor
cout << "Type one dimension of the floor in feet: ";
cin >> dimension;
return dimension;
}
//
// Function to calculate the floor space (area of the floor)
// given two dimensions (length and width).
// Pre: dim1 and dim2 are integer dimensions.
// Post: The function has returned the integer floor space.
//
int area(int dim1, int dim2)
{
return (dim1 * dim2);
}