SOURCE FILE: floorSpace.cpp


// Example 4.7. Program to display the floor space for a room   

/* Notice here how the if-then-else logic is used to handle a possible error
in the form of the input. */

#include <iostream>
using namespace std ;

void directions(void); // prototypes
int GetDim(void);
int area(int, int);

int main(void)
{
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 << endl ;
cout << "Sorry, only positive dimensions are allowed.";
cout << endl ;
cout << "Please reexecute the program.";
cout << endl ;
}
else // valid data
{
FloorSpace = area(dim1, dim2);
cout << endl << "The floor space is "
<< FloorSpace << " square feet." << endl ;
cout << endl ;
}

return 0;
}

//
// Function to print program directions
// Pre: none
// Post: Program directions have been printed.
//

void directions(void)
{
cout << endl ;
cout << "This program will give you the floor space ";
cout << "in square feet" << endl ;
cout << "for a room of your choosing." << endl << endl ;
}

//
// 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);
}