Please help, I am writing a program to take in 3 values and return the shape of a triangle. The program runs, but not quite right. Instead of returning the shape of a triangle, it returns a smiley face....which really surprised me. Can anybody help? The code I have is:

#include <iostream>

using namespace std;


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

char triangleShape (int&, int&, int&);

 
int main()
{

    int side1, side2, side3;
    char shape;


	cout<<"Please enter the desired length of the sides of the triangle."<<endl;
	cin>> side1>>side2>>side3;


    shape = triangleShape (side1, side2, side3);


	cout<<"The shape of the triangle is: "<<shape<<endl;


	return 0;
}


char triangleShape (int& side1, int& side2, int& side3)
{
    char shape;
    


	if (side1==side2 && side2==side3)
		shape = equilateral;
	else if (side1==side2 || side1==side3 || side2==side3)
		shape = isosceles;
	else if (side1!=side2 && side1!=side3)
		shape = scalene;
	else if (side1+side2<side3 || side1+side3<side2 || side2+side3<side2)
		shape = noTriangle;

    return shape;	

}

Recommended Answers

All 2 Replies

Enumerations are just convenient sets of named integers. For example, in your code, scalene is 0, isosceles is 1, equilateral is 2, and noTriangle is 3. Printing out one of those low values as a char will likely give you something funky. You can fix it by returning a string instead:

#include <iostream>

using namespace std;

enum triangleType {scalene, isosceles, equilateral, noTriangle};
char *tostr[] = {"scalene", "isosceles", "equilateral", "noTriangle"};

char *triangleShape (int&, int&, int&);

int main()
{
  int side1, side2, side3;
  char *shape;

  cout<<"Please enter the desired length of the sides of the triangle."<<endl;
  cin>> side1>>side2>>side3;
  shape = triangleShape (side1, side2, side3);
  cout<<"The shape of the triangle is: "<<shape<<endl;

  return 0;
}

char *triangleShape (int& side1, int& side2, int& side3)
{
  char *shape;

  if (side1==side2 && side2==side3)
    shape = tostr[equilateral];
  else if (side1==side2 || side1==side3 || side2==side3)
    shape = tostr[isosceles];
  else if (side1!=side2 && side1!=side3)
    shape = tostr[scalene];
  else if (side1+side2<side3 || side1+side3<side2 || side2+side3<side2)
    shape = tostr[noTriangle];

  return shape;
}

Wow, that helped a lot.....thanks so much.

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.