### Simple function trouble

Hello, in class we are learning about enumerated data types and were asked to complete a program to figure out if a triangle is equilateral, isosceles, scalene or just not a triangle.

pretty simple, i get no error messages but my output function does not work, if someone could guide me in the right direction it would be much appreciated.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889`` ``````#include using namespace std; enum triangles {equilateral, isosceles, scalene, notriangle, triangle}; void getlengths(double & side1, double & side2, double & side3); triangles determineshape(double side1, double side2, double side3, triangles triangle); void displayshape(triangles triangle); int main() { char response; double side1, side2, side3; do { getlengths(side1, side2, side3); determineshape(side1, side2, side3, triangle); displayshape(triangle); cout << "Do you wish to continue (Y or N) : "; cin >> response; response = toupper (response); } while (response == 'Y'); return 0; } void getlengths(double & side1, double & side2, double & side3) { cout << "input the lengths of side 1" << endl ; cin >> side1; cout << "input the lengths of side 2" << endl ; cin >> side2; cout << "input the lengths of side 3" << endl ; cin >> side3; } triangles determineshape(double side1, double side2, double side3, triangles triangle) { if ((side1 + side2) < (side3)) triangles triangle = notriangle; else if ((side2 + side3) < (side1)) triangles triangle = notriangle; else if ((side1 + side3) < (side2)) triangles triangle = notriangle; else if (side1==side2 && side2 == side3) triangles triangle = equilateral; else if (side1==side2 || side1==side3 || side2==side3) triangles triangle = isosceles; else triangles triangle = scalene; return triangle; } void displayshape(triangles triangle) { switch(triangle) { case notriangle: cout << "not a real triangle"; break; case equilateral: cout << "this is an equilateral triangle"; break; case isosceles: cout << "this is an isosceles triangle"; break; case scalene: cout << "this is a scalene triangle"; break; } } ``````
OK, your determineshape function has some problems. Firstly, you are supplying it with the VALUE of "triangle", or 4. As far as I can see, you don't need that at all. Secondly, you are declaring your triangle inside of if statements, which have their own individual scope, and then returning the value passed to you by the main() function, or 4. Therefore, every time, you will not be outputting anything, due to the displayshape function only testing 0-3, and you giving it 4.

 ``123456789101112131415161718192021222324252627`` ``````enum triangles { equilateral, isosceles, scalene, notriangle, // Don't need "triangle", why did you have it anyway? }; // Don't need to supply it with the "triangles" enum triangles determineShape(double side1, double side2, double side3); // etc... triangles determineShape(double side1, double side2, double side3) { triangles result; // Initialise your return here if (side1 + side2 < side3) result = notriangle; else if (side1 + side3 < side2) result = notriangle; // etc... return result; }``````
Last edited on
I'm sort of having trouble understanding what you're trying to explain (please excuse new coder).

I tweaked the code to the best of my understanding of what you're trying to explain

1>c:\users\michael\desktop\ayler22.cpp(25): warning C4700: uninitialized local variable 'result' used

line 25, unsure where it wants it initialized?

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889`` ``````#include using namespace std; enum triangles {equilateral, isosceles, scalene, notriangle}; void getlengths(double & side1, double & side2, double & side3); triangles determineshape(double side1, double side2, double side3); void displayshape(triangles result); int main() { char response; double side1, side2, side3; triangles result; do { getlengths(side1, side2, side3); determineshape(side1, side2, side3); displayshape(result); cout << "Do you wish to continue (Y or N) : "; cin >> response; response = toupper (response); } while (response == 'Y'); return 0; } void getlengths(double & side1, double & side2, double & side3) { cout << "input the lengths of side 1" << endl ; cin >> side1; cout << "input the lengths of side 2" << endl ; cin >> side2; cout << "input the lengths of side 3" << endl ; cin >> side3; } triangles determineshape(double side1, double side2, double side3) { triangles result; if (side1 + side2 < side3) result = notriangle; else if (side2 + side3 < side1) result = notriangle; else if (side1 + side3 < side2) result = notriangle; else if (side1==side2 && side2 == side3) result = equilateral; else if (side1==side2 || side1==side3 || side2==side3) result = isosceles; else result = scalene; return result; } void displayshape(triangles result) { switch(result) { case notriangle: cout << "not a real triangle"; break; case equilateral: cout << "this is an equilateral triangle"; break; case isosceles: cout << "this is an isosceles triangle"; break; case scalene: cout << "this is a scalene triangle"; break; } }``````
Topic archived. No new replies allowed.