Sunday, January 1, 2017

Programming Challenge 6.2 - Rectangle Area Complete

/* Rectangle Area Complete - This program asks the user to enter the
   width and length of a rectangle and then displays the rectangle's
   area.
  
   The program calls the following functions:
  
   * getLength: This function asks the user to enter the rectangle's
     length and then returns that value as a double.

   * getWidth: This function asks the user to enter the rectangle's
     width and then returns that value as a double.

   * getArea: This function accepts the rectangle's length and width
     as arguments and returns the rectangle's area.
   
     * The area is calculated by multiplying the length by the width.

   * displayData: This function accepts the rectangle's length, width,
     and area as arguments and displays them in an appropriate message
     on the screen. */

#include "Utility.h"

/* Prototypes: Get length, Get width, Get area, Display data */
double getLength(double);
double getWidth(double);
double getArea(double, double);

void displayData(double, double, double);

int main()
{
   /* Variables: Length, Width, Area */
   double length = 0.0,
          width = 0.0,
          area = 0.0;

   /* Display: Information */
   cout << "This program allows you to easily calculate the\n"
        << "area of a rectangle, by providing the length and\n"
        << "width values. Once done, the program will display\n"
        << "the area.\n";

   /* Get: Length of the rectangle
      Call: getLength */
   length = getLength(length);

   /* Get: Width of the rectangle
      Call: getWidth */
   width = getWidth(width);

   /* Get: Area of the rectangle
      Call: getArea */
   area = getArea(length, width);

   /* Display: Rectangle data
      Call: displayData    */
   displayData(length, width, area);

    pauseSystem();
    return 0;
}

/* **********************************************************
   Definition: getLength

   This function asks the user for the rectangle's length,
   stores the value in lenRect and returns the value to main.
   ********************************************************** */

double getLength(double lenRect)
{
   /* Variable: Rectangle length */
   double rectLength = 0.0;

   /* Get: The rectangle length */
   cout << "\nPlease enter the rectangle's length: ";
   cin >> rectLength;

   /* Validation: While the user enters a negative value, an
      error message will be displayed, and the user is asked
      to repeat his or her input */
   while (rectLength < 0)
   {
      cout << "Sorry, only positive values for the rectangle's\n"
           << "length are accepted.\n"
           << "Please enter the rectangle's length: ";
      cin >> rectLength;
   }

   /* Return: Rectangle length */
   return rectLength;
}

/* **********************************************************
   Definition: getWidth

   This function asks the user for the rectangle's width,
   stores the value in rectWidth and returns this value to
   main.
   ********************************************************** */

double getWidth(double wdRect)
{
   /* Variable: Rectangle width */
   double rectWidth = 0.0;

   /* Get: The rectangle width */
   cout << "Please enter the rectangle's width: ";
   cin >> rectWidth;

   /* Validation: While the user enters a negative value, an
   error message will be displayed, and the user is asked
   to repeat his or her input */
   while (rectWidth < 0)
   {
      cout << "Sorry, only positive values for the rectangle's\n"
           << "width are accepted.\n"
           << "Please enter the rectangle's width: ";
      cin >> rectWidth;
   }

   /* Return: Rectangle width */
   return rectWidth; 
}

/* **********************************************************
   Definition: getArea

   This function accepts the rectangle's length and width as
   arguments. It returns the result of the calculation.
   ********************************************************** */

double getArea(double lenRect, double wdRect)
{
   /* Return: Result of calculation */
   return lenRect * wdRect;
}

/* **********************************************************
   Definition: displayData

   This function accepts the rectangle's length, width, and
   area. It displays them on screen.
   ********************************************************** */

void displayData(double lenRect, double wdRect, double areaRect)
{
   /* Display: Rectangle data */
   cout << "\nA Rectangle with the following data:\n"
        << "-------------------------------------\n"
        << "\nLength: " << lenRect << " cm\n"
        << "Width:  " << wdRect << " cm\n\n"
        << "-------------------------------------\n"
        << "Has an area of " << areaRect << " square cm.\n";
}

No comments:

Post a Comment