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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
#include <iostream>
#include <string>
using namespace std;
enum triangle {scalene, isosceles, equilateral, notriangle};
void getlengths(double& length1, double& length2, double& length3);
void calcshapes(double length1, double length2, double length3, triangle& shape);
void displayshape(triangle shape);
//Call each function and keep program going according to user command via loop.
int main()
{
triangle shape;
double length1, length2, length3;
char response;
do
{
cout << "Do you wish to continue? (Y/N) ";
cin >> response;
response = toupper (response);
getlengths(length1, length2, length3);
calcshapes(length1, length2, length3, shape);
displayshape(shape);
}
while (response == 'Y' || response == 'y');
system("pause");
return 0;
}
//Obtain length of each side via user input.
void getlengths(double& length1, double& length2, double& length3)
{
cout << "Length of the first side: " << endl;
cin >> length1;
cout << "Length of the second side: " << endl;
cin >> length2;
cout << "Length of the third side: " << endl;
cin >> length3;
}
//Use relationship between side lengths to determine type of triangle.
void calcshapes(double length1, double length2, double length3, triangle& shape)
{
if (!length1 + length2 > length3 || !length1 + length3 > length2 || !length2 + length3 > length1)
{
shape = notriangle;
}
else if (length1 == length2 && length1 == length3)
{
shape = equilateral;
}
else if (length1 == length2 || length1 == length3 || length2 == length3)
{
shape = isosceles;
}
else
shape = scalene;
}
//Display the shape determined by prior function.
void displayshape(triangle shape)
{
if (shape = notriangle)
{
cout << "The figure is not a triangle.";
}
else if (shape = equilateral)
{
cout << "The figure is an equilateral triangle.";
}
else if (shape = isosceles)
{
cout << "The figure is an isosceles triangle.";
}
else if (shape = scalene)
{
cout << "The figure is a scalene triangle.";
}
}
|