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;
}

Dani AI

Generated

The posted program fails for a few simple, fixable reasons: the side lengths and the result are never shared correctly between main and the function (so both shape and the function’s A,B,C are uninitialized), the scalene test uses OR instead of AND (so it becomes true too often), and there’s no check for positive lengths or the triangle inequality (so degenerate or invalid input can be classified incorrectly). spotted the uninitialized-value symptom, pointed out pass-by-value vs returning/a reference, and ’s suggestion to return an enum is the clean direction.

A robust approach: write a small classifier that accepts three numeric lengths (use double), validates positivity and the triangle inequality, compares with a small tolerance (epsilon) for floating-point equality, and returns an enum result. Returning the enum keeps call sites simple and avoids shadowing or uninitialized variables.

Example (classification function + usage sketch — different from earlier snippets):

enum TriKind { TK_Invalid, TK_Equilateral, TK_Isosceles, TK_Scalene };

TriKind classify(double a, double b, double c) {
    const double eps = 1e-9;
    if (a <= eps || b <= eps || c <= eps) return TK_Invalid;
    if (a + b <= c + eps || a + c <= b + eps || b + c <= a + eps) return TK_Invalid;
    bool ab = fabs(a - b) <= eps;
    bool bc = fabs(b - c) <= eps;
    bool ac = fabs(a - c) <= eps;
    if (ab && bc) return TK_Equilateral;
    if (ab || bc || ac) return TK_Isosceles;
    return TK_Scalene;
}

// call: TriKind k = classify(x, y, z);

Troubleshooting notes: keep types consistent (no mixing int and float), handle input errors (cin >> failure), choose epsilon appropriate to expected magnitudes, and treat a+b == c as invalid (degenerate) unless the assignment specifies otherwise. This addresses the core bugs in ’s code while incorporating the good points made by and .

Recommended Answers

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 meeting, networking, learning, and sharing knowledge.