Lab05b - Triangle Formulas

About this Page

The user inputs the real number lengths of three sides of a triangle. The program calculates the area and perimeter using the values entered by the user and the formulas for perimeter and for area. The lengths of the three sides, the perimeter, and the area are printed on the user's screen.

/* Title     - Triangle Formulas
 * File name - Lab05
 * Programmer- 415 Erich Musick
 * IPO       - Input - The user inputs the real number lengths of three sides of
                a triangle.
               Processing - The program calculates the area and perimeter using
                the values entered by the user and the formulas for perimeter
                and for area.
               Output - The lengths of the three sides, the perimeter, and the
                area are printed on the user's screen.
*/

//---------------------Start Header Files------------------------------------

//#include "iostream.h"
#include "waittoclose.h"  // Add to project waittoclose.cpp
#include "apstring.h"     // Add to project apstring.cpp
//#include "randgen.h"      // Add to project randgen.cpp
#pragma hdrstop
//#include "apvector.h"     // DO NOT ADD TO PROJECT apvector.cpp
//#include "apmatrix.h"     // DO NOT ADD TO PROJECT apmatrix.cpp

//----------------------End Header Files-------------------------------------

//-------------------------Start Main----------------------------------------
int main() {
  double sideA;             // Declare some variables
  double sideB;
  double sideC;
  double perimeter;
  double s;
  double area;
  const apstring colSep = "   "; // Column Seperator
  apstring another;
  int count;
  int y;
  count = 0;

  // Tell what the program is about
  cout << "This program will calculate the perimeter and area of any triangle. "
       << "The user\nonly needs to enter the real-number lengths of the sides of"
       << " a triangle. The\nprogram DOES NOT protect against values that do not"
       << " form a triangle.\n"
       << endl;
  do {
    if (count == 0) {
      // Get the lengths of the sides of the triangle
      cout << "Enter the length of side a: ";
      cin >> sideA;
      cout << "\n"
           << "Enter the length of side b: ";
      cin >> sideB;
      cout << "\n"
           << "Enter the length of side c: ";
      cin >> sideC;
    } else {
      gotoxy(29,9);
      clreol();
      gotoxy(29,11);
      clreol();
      gotoxy(29,7);
      clreol();
      cin >> sideA;
      gotoxy(29,9);
      cin >> sideB;
      gotoxy(29,11);
      cin >> sideC;
    }
    // Calculate the permiter
    perimeter = sideA + sideB + sideC;

    // Calculate the area using hero's formula
    s = (sideA + sideB + sideC) / 2;
    area = sqrt(s*(s-sideA)*(s-sideB)*(s-sideC));
    cout << endl
         << "Another (y/n)? ";
    if (count == 0) {
      gotoxy(1,14);
      // Display the lengths of each side, the perimeter, and the area
      cout << "\n"
       << setw(10)
       << "Side A  "
       << colSep
       << setw(10)
       << "Side B  "
       << colSep
       << setw(10)
       << "Side C  "
       << colSep
       << setw(11)
       << "Perimeter "
       << colSep
       << setw(10)
       << "Area   "
       << endl
       << setw(10)
       << "----------"
       << colSep
       << setw(10)
       << "----------"
       << colSep
       << setw(10)
       << "----------"
       << colSep
       << setw(11)
       << "----------"
       << colSep
       << setw(10)
       << "----------"
       << colSep
       << endl;
    }
    y = 17 + count;
    gotoxy(1,y);
    cout << setw(10)
         << sideA
         << colSep
         << setw(10)
         << sideB
         << colSep
         << setw(10)
         << sideC
         << colSep
         << setw(11)
         << perimeter
         << colSep
         << setw(10)
         << area
         << endl;
    if (count != 9) {
      gotoxy(16,13);
      clreol();
      cin >> another;
    }
    count++;
  } while (another == "y" && count < 10);
  gotoxy(1,13);
  clreol();
  y += 2;
  gotoxy(1,y);
  return 0;
}
//--------------------------End Main-----------------------------------------

Return to C++ Snippetts