programming with "enum"

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 07-16-2005
MyntiFresh
programming with "enum"
Hey... I've been working on this program using the enumeration type. When I execute what I've written, it returns a number instead of the type of triangle it is. If you could look at the code and help me out I'd appreciate it!

Code:

```#include <iostream> using namespace std; enum triangleType {scalene, isosceles, equilateral, noTriangle}; triangleType triangleShape(double, double, double); int main() {         double firstSide, secondSide, thirdSide;                 cout << "Enter the lengths of the three sides of a triangle." << endl;         cin >> firstSide >> secondSide >> thirdSide;         cout << endl;         cout << "The shape of the triangle is: "                 << triangleShape(firstSide, secondSide, thirdSide) << endl;         return 0; } triangleType triangleShape(double firstSide, double secondSide, double thirdSide) {         triangleType type;         if (firstSide + secondSide <= thirdSide || firstSide + thirdSide <= secondSide ||                 secondSide + thirdSide <= firstSide)         {                 type = noTriangle;         }         if (firstSide + secondSide > thirdSide && firstSide + thirdSide > secondSide &&                 secondSide + thirdSide > firstSide)         {                 if (firstSide != secondSide && firstSide != thirdSide && secondSide != thirdSide)                 {                         type = scalene;                 }                 else if (firstSide == secondSide || firstSide == thirdSide || secondSide == thirdSide)                 {                         type = isosceles;                 }                 else                 {                         type = equilateral;                 }         }         return type; }```
• 07-16-2005
cboard_member
enum's are enumerated constants. They represent numbers. The first entry in an enum statement represents 0, the second 1 and so on.

You can of course change this:

Code:

`enum triangles { equilateral=10, scalene=20 }; // etc`
If this isn't what you meant, well, sorry.
• 07-16-2005
MyntiFresh
No, that's not what I meant but thanks for tryin. In my function I'm trying to return the type of triangle after the user has input the lengths of the three sides of a triangle. But when I run the program, it's giving me those values; 0, 1, 2, etc. Any one got some ideas for me?!?!
• 07-16-2005
cboard_member
When the user inputs the # sides for a scalene triangle, what number is output?
• 07-16-2005
mrafcho001
well yeah.. it is suppose to return a number..
Code:

```enum triangleType {scalene, isosceles, equilateral, noTriangle}; //scalene = 0; // isosceles = 1; // equilateral = 2; //noTriangle = 3;```
and the function is going to return one of those values.

if you want it return a string like "Equilateral" make a string function
• 07-16-2005
cboard_member
• 07-16-2005
mrafcho001
Quote:

Originally Posted by ahluka

sorry i didn't see your reply.. you must of posted it seconds before mine..
• 07-16-2005
cboard_member
Well you replied to my reply saying thanks for trying. Never mind about it, at least you won't have problems with it again :)

*edit*

Damn, sorry, I thought you were the one who started the thread lol. Yeah I posted it about the same time
• 07-16-2005
MyntiFresh
Quote:

Originally Posted by mrafcho001
well yeah.. it is suppose to return a number..
Code:

```enum triangleType {scalene, isosceles, equilateral, noTriangle}; //scalene = 0; // isosceles = 1; // equilateral = 2; //noTriangle = 3;```
and the function is going to return one of those values.

if you want it return a string like "Equilateral" make a string function

But for an enumeration type, aren't "scalene, isosceles, etc" variables, not strings? How would I set that up if making it return a string is the only way I can do it?
• 07-16-2005
mrafcho001
first start off with making the function return a string:
Code:

```#include <iostream> using namespace std; string Triangle(double side1, double side2, double side3) {     //Here check the sides like you did on the other function   //and return the right string for examlpe for Equilateral triangle "Equilateral" } int main() { return 0; }```
• 07-16-2005
laserlight
Well, you could do something like this just after the enum:
Code:

`std::string triangleName[] = {"scalene", "isosceles", "equilateral", "noTriangle"};`
After which triangleName[isosceles] would be the string "isosceles".
• 07-16-2005
MyntiFresh
The point of my program though is on using the enumeration type. Shouldn't there be another way for me to return what I want it to?
• 07-16-2005
JoshR
Code:

```string trianglePrint (triangleType t) {   switch(t)   {       case scalene: return "scalene";       case isosceles: return "isosceles";       case equilateral: return "equilateral";       case noTriangle: return "No Triangle";       default: return "Error";   } }```
Or you can directly print it...
Code:

```void trianglePrint (triangleType t) {   switch(t)   {       case scalene: cout << "scalene"; break;       case isosceles: cout << "isosceles"; break;       case equilateral: cout << "equilateral"; break;       case noTriangle: cout << "No Triangle"; break;       default: cout << "Error";   } }```
• 07-16-2005
MyntiFresh
I'm supposed to be able to return the shape of the triangle from the triangleShape function.
• 07-16-2005
cboard_member
You're not making much sense here. Do you want to return a string indicating which shape it is (the examples fellow repliers have posted above) or, like, a vertex for use in '3d space'.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last