Triangle Perimeter/Area Calculator Problem
I have a messed up perimeter value when I try to compute the perimeter, can someone point out to me what I'm doing wrong?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
|
//Ch14Lab3.cpp-calculates and displays the perimeter and area of a triangle
#include <iostream>
#include <iomanip>
#include "c:\Users\Downloads\TriangleClass.h"
using std::cout;
using std::cin;
using std::endl;
using std::setprecision;
using std::ios;
using std::setiosflags;
int main()
{
//create Triangle object
Triangle triangle;
//declare variables
double triangleBase = 0.0;
double triangleSideone = 0.0;
double triangleSidetwo = 0.0;
double triangleHeight = 0.0;
double triangleArea = 0.0;
double trianglePerimeter = 0.0;
//get length, width, and sod price
cout << "Base: ";
cin >> triangleBase;
cout << "Side: ";
cin >> triangleSideone;
cout << "Other Side: ";
cin >> triangleSidetwo;
cout << "Height: ";
cin >> triangleHeight;
//assign input ot Rectangle object
triangle.SetDimensions(triangleBase, triangleSideone, triangleSidetwo, triangleHeight);
//calculate area and the total price
triangleArea = triangle.CalcArea();
trianglePerimeter = triangle.CalcPerimeter();
//display area and total price
cout << setiosflags(ios::fixed) << setprecision(0);
cout << "Area: " << triangleArea << endl;
cout << "Perimeter: " << trianglePerimeter << endl;
return 0;
} //end of main function
| |
Class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
//Rectangle Class.cpp-calculates and displays the cost of laying sod
//declaration section
class Triangle
{
public:
Triangle();
void SetDimensions(double, double, double, double);
double CalcArea();
double CalcPerimeter();
private:
double base;
double sideOne;
double sideTwo;
double height;
};
//implementation section
Triangle::Triangle()
{
base = 0.0;
sideOne = 0.0;
sideTwo = 0.0;
height = 0.0;
} //end of default constructor
void Triangle::SetDimensions(double b, double sido, double sidt, double h)
{
//assigns length and width to private data members
if (b > 0.0 && sido > 0.0 && sidt > 0.0 && h > 0.0)
{
base = b;
sido = sideOne;
sidt = sideTwo;
height = h;
} //end if
} //end of SetDimensions method
double Triangle::CalcArea()
{
return 0.5 * base * height;
} //end of CalcArea function
double Triangle::CalcPerimeter()
{
return base + sideOne + sideTwo;
} //end of CalcPerimeter method
| |
Base: 1
Side: 1
Other Side: 1
Height: 2
Area: 1
Perimeter: 1
Press any key to continue . . . |
Perimeter should be 3 but I got 1.
Topic archived. No new replies allowed.