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.

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>

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.

Try this instead:
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
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?

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>

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.