|
-
November 17th, 2015, 03:07 PM
#1
Assistance with enumeration in combination with user-defined functions.
Hello:
I am having a little trouble with an assignment that involves enumeration. While my code seems to work, my instructor informed me that I am only to use user-defined function, with for statements, while loops, switch statements in the main body. Embarrassingly, for whatever reason, I can't seem to wrap my head how I am supposed to rewrite my switch statement as a user-defined function.
Here is what the assignment is asking for:
1a) Define an enumeration type, triangleType, that has the value 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 thed 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).
c) Write a program that prompts the user to input the length of the side of a triangle and outputs the shape of the triangle.
And here is what I have so far:
Code:
#include <iostream>
using namespace std;
//user-defined (enumerator) data type
typedef int side;
enum triangleType { scalene, isosceles, equilateral, noTriangle } shape;
void sideTri(side&, side&, side&);
triangleType triangleShape(side&, side&, side&, triangleType&);
int main() {
side a;
side b;
side c;
int type;
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;
}
void sideTri(side& a, side& b, side& c)
{
cout << "Enter side a: ";
cin >> a;
cout << "Enter side b: ";
cin >> b;
cout << "Enter side c: ";
cin >> c;
}
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;
}
Any help is greatly appreciated. Thank you.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|