I'm writing a program that asks the user to input 3 numbers, then outputs the shape of the triangle. This is what I have so far, but it still doesn't work.

Can someone help?

Thanks!

``````#include <iostream>
using namespace std;

enum triangleType {scalene, isosceles, equilateral, noTriangle};

void triangleShape (int shape);

int main()
{
float A, B, C;
int shape;

cout<<"Enter the length of three sides of a triangle"<<endl;
cin>>A;
cin>>B;
cin>>C;
triangleShape (shape);
if (shape == equilateral)
cout<<"The shape of the triangle is: equilateral";
else if (shape == isosceles)
cout<<"The shape of the triangle is: isosceles";
else if (shape == scalene)
cout<<"The shape of the triangle is: scalene";
else
cout<<"The shape of the triangle is: noTriangle";
cout<<endl;
return 0;
}

void triangleShape (int shape)  //my instructor said to make shape of
//type triangleShape, but I'm not sure
//what that means, plz explain
{
int A, B, C;
int shape;

if ((A == B)&&(B == C))
shape = equilateral;
else  if ((A == B)||(B == C)||(A == C))
shape = isosceles;
else if ((A != B)||(B != C)||(A != C))
shape = scalene;
else
shape = noTriangle;
}``````

In main(), where does shape get its value? In triangleShape() where do A, B, and C get their values -- and which shape do you mean?

## All 3 Replies

In main(), where does shape get its value? In triangleShape() where do A, B, and C get their values -- and which shape do you mean?

Fist thing you need to do is change the prototype of triangleShape. Since it has to usethe lengths of the triangle you need to pass them into it. It needs to look like

``....triangleShape(float A, float B, float C)``

I think your instructor means you to have shape of type triangleType. Since triangleShape works out the correct val for shape, you will need to return it, so that you can do the outputting. So, the prototype for triangleShape is

``triangleType triangleShape(float A, float B, float C);``

The main will look something like

``shape = triangleShape(float A, float B, float C);``

You may also want to think about what happens in your func if a side/sides has zero length for example if A == B == C == 0, is it equilateral or is invalid?

To get to the core of why you don't get your expected result:

Check what happens in your function triangleShape(int).
Because you are passing your shape variable to the method by value any changes to the shape parameter of the method are not visible outside the method.
To get the shape known outside the method, either give the method a return type int and use that to set your shape variable to the value calculated or make the method parameter a reference (int&). In that case you should also remove the local variable shape from the method.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.