SOURCE FILE: floorSpace.cpp
/*
Program to display the floor space for a room
Notice here how the if-else logic is used to handle a possible error in the
form of the input.
*/
#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
directions();
dim1 = GetDim();
dim2 = GetDim();
// only process positive floor dimensions
if ( (dim1 <= 0) || (dim2 <= 0) ) // invalid data
{
// The user typed 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 DEFINITIONS */
/* ================================== */
/* ================================== */
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 ;
}
/* ================================== */
/* ================================== */
int GetDim(void)
{
int dimension; // one dimension of floor
cout << "Type one dimension of the floor in feet: ";
cin >> dimension;
return dimension;
}
/* ================================== */
/* ================================== */
int area(int dim1, int dim2)
{
return (dim1 * dim2);
}
/* ================================== */