I'm really confused on some homework of mine. The code is working; however, its supposed to determine what type of triangle three variable sides create.
Here is the problem out of the book:
(A) Define an enumeration type, triangleType, that has the values scalene, isosceles, equilateral, and noTriangle.
(B) Write a function, triangleShape, that takes as parameters three numbers, each of which represents the length of a side of the triangle. The function should return the shape of the triangle. [Note: In a triangle, the sum of the lengths of any two sides is greater than the length of the third side]
© Write a program that prompts the user to input the length of the sides of a triangle and outputs the shape of the triangle.
#include <iostream>
using namespace std;
typedef int side;
enum triangleType {scalene, isosceles, equilateral, noTriangle} shape;
triangleType triangleShape(side&, side&, side&, triangleType&);
int main() {
side a;
side b;
side c;
int type;
cout << "Enter side a: ";
cin >> a;
cout << "Enter side b: ";
cin >> b;
cout << "Enter side c: ";
cin >> c;
type = triangleShape(a, b, c, shape);
switch(type) {
case 0:
cout << "This is a scalene triangle." << endl;
break;
case 1:
cout << "This is an isosceles triangle." << endl;
break;
case 2:
cout << "This is an equilateral triangle." << endl;
break;
case 3:
cout << "This is not a triangle." << endl;
break;
}
return 0;
}
triangleType triangleShape(side& a, side& b, side& c, triangleType& shape) {
if (a == b && b == c) return equilateral;
else if (a == b || b == c || c == a) return isosceles;
else if (a != b && b != c && c != a) return scalene;
else return noTriangle;
}
The code works fine and returns the correct values. However, the "noTriangle" value... Is there something I'm missing on this? How would I determine if it's "not a triangle"?
Edited by Jrb, 03 April 2011 - 12:36 AM.


Sign In
Create Account


Back to top









