Hey guys, here's the question I'm asked.

a.
Define an enumeration type, triangleType, that has the values scalene, isosceles, euilateral, and noTriangle.

b.
Write a function, triangleShape, that takes as parameters three numbers, each of which represents the length of a side of the triangle. The function should return the shape of the triangle.

c.
Write a program that prompts the user to input the length of the sides of a triangle and outputs the shape of the triangle.

Here is my code:

#include <iostream>

using namespace std ;

enum triangleType { scalene , isosceles , equilateral , noTriangle } ;
triangleType triang ( double , double , double ) ;

int main()
{
    double a ;
    double b ;
    double c ;

    int shape ;

    cout << "Side 'a' length:  " << flush ;
    cin >> a ;
    cout << "Side 'b' length:  " << flush ;
    cin >> b ;
    cout << "Side 'c' length:  " << flush ;
    cin >> c ;

    shape = triang ( a , b , c ) ;
    switch (shape)
    {
    case 0:
        cout << "Triangle Shape:  Scalene" << endl ;
        break ;
    case 1:
        cout << "Triangle Shape:  Isosceles" << endl ;
        break ;
    case 2:
        cout << "Triangle Shape:  Equilateral" << endl ;
        break ;
    case 3:
        cout << "Triangle Shape:  -- No Shape --" << endl ;
        break ;
    }

    return 0 ;
}

triangleType triang ( double a , double b , double c )
{

    triangleType shape ;

    if ( ( a > (b + c) ) || ( b > (a + c) ) || ( c > (a + b) ) )
        shape = noTriangle ;        
    else if ( ( a == b ) && ( a == c ) && ( b == c ) )
        shape = equilateral ;
    else if ( a != b && a != c && b != c )
        shape = scalene ;
    else
        shape = isosceles ;

    return shape ;
}

It works, I just want to know if I'm answering the question correctly?

>>It works, I just want to know if I'm answering the question correctly?

If the program works ok, then yes you probably answered the problem correctly.

The one problem I see is that you can't compare floating point numbers directly. Here's a re-write with slightly different logic (you can keep yours if you want, they're about the same) and a better way to compare the doubles:

#include <cmath>
#include <limits>
triangleType triang(double a, double b, double c)
{
  const double eps = std::numeric_limits<double>::epsilon();
  triangleType shape = scalene;
  if( fabs(a - b) <= eps && fabs(a - c) <= eps)
    shape = equilateral;
  else if( fabs(a-b) <= eps || fabs(a - c) <= eps)
    shape = isosceles;
  else if ( a > b + c || b > a + c || c > a + b) // this could be first like you had it
    shape = noTriangle;
  return shape;
}

Admittely, I just copied the epsilon thing from some google result, so I don't know if that's the correct way to go about it.

commented: Helpful and Logical posts -duki +1

thanks infarction. I would use this, but we havn't gone over any limits and are just studying namespaces this chapter. Thanks though =)


I'll just go with it, thanks dragon.

Another thing to consider is the line else if ( ( a == b ) && ( a == c ) && ( b == c ) ) is a little longer than it needs to be. If a == b and a == c, isn't b automatically equal to c?
Therefore, only 2 terms are necessary. This of course does apply to !=...

thanks infarction. I would use this, but we havn't gone over any limits and are just studying namespaces this chapter. Thanks though =)


I'll just go with it, thanks dragon.

The limits thing was just to pull out a compiler specified degree of accuracy. Since floating point numbers aren't stored precisely in memory, you can't use == and != with much accuracy. You can specify your own degree of accuracy and do similar to my post to check if numbers are [un]equal (within that degree of accuracy).

Hey guys, here's the question I'm asked.

a.
Define an enumeration type, triangleType, that has the values scalene, isosceles, euilateral, and noTriangle.

b.
Write a function, triangleShape, that takes as parameters three numbers, each of which represents the length of a side of the triangle. The function should return the shape of the triangle.

c.
Write a program that prompts the user to input the length of the sides of a triangle and outputs the shape of the triangle.

Here is my code:

#include <iostream>
#include <iomanip>
using namespace std ;
enum triangleType { scalene , isosceles , equilateral , noTriangle } ;
triangleType angle (double a, double b, double c);
void showshape(int what_shape);
void lfmHead();
int main()
{
//Data Dictionary
double a ;
double b ;
double c ;
int shape ;
//User input for three sides of a trianle
cout << "Side 'a' length: ";
cin >> a ;
cout << "Side 'b' length: ";
cin >> b ;
cout << "Side 'c' length: ";
cin >> c ;

lfmHead(); //Header for exercise
shape = angle ( a , b , c ) ; //Function determines what type of triangle is created
//Display numbers entered for triangle
cout << "\nThe numbers entered for the sides of the triangle: "
<< a << " " << b << " " << c << endl;
//Display type of trianle created
showshape(shape);
return 0 ;
}
triangleType angle (double a , double b , double c)
{
triangleType shape ;
if ( ( a > (b + c) ) || ( b > (a + c) ) || ( c > (a + b) ) )
shape = noTriangle ;
else if ( ( a == b ) && ( a == c ) && ( b == c ) )
shape = equilateral ;
else if ( a != b && a != c && b != c )
shape = scalene ;
else
shape = isosceles ;
return shape ;
}
void showshape(int what_shape)
{
switch (what_shape)
{
case 0:
cout << "Triangle Shape: Scalene" << endl ;
break ;
case 1:
cout << "Triangle Shape: Isosceles" << endl ;
break ;
case 2:
cout << "Triangle Shape: Equilateral" << endl ;
break ;
case 3:
cout << "Triangle Shape: -- No Shape --" << endl ;
break ;
}
}
void lfmHead()
{ cout << "\n\t\tOutput for Ch 8 Pex 1" << endl;
cout << setfill('*') << setw(57) << "\n";
}
/*
***** First Run *****
Side 'a' length: 1
Side 'b' length: 2
Side 'c' length: 3
Output for Ch 8 Pex 1
********************************************************
The numbers entered for the sides of the triangle: 1 2 3
Triangle Shape: Scalene
***** Second Run *****
Side 'a' length: 1
Side 'b' length: 1
Side 'c' length: 2
Output for Ch 8 Pex 1
********************************************************
The numbers entered for the sides of the triangle: 1 1 2
Triangle Shape: Isosceles
***** Third Run *****
Side 'a' length: 5
Side 'b' length: 5
Side 'c' length: 5
Output for Ch 8 Pex 1
********************************************************
The numbers entered for the sides of the triangle: 5 5 5
Triangle Shape: Equilateral
***** Final Run *****
Side 'a' length: 5
Side 'b' length: 3
Side 'c' length: 1
Output for Ch 8 Pex 1
********************************************************
The numbers entered for the sides of the triangle: 5 3 1
Triangle Shape: -- No Shape --
*/

This is my rewrite of code presented with added functions

> Here is my code:
Yuck. Learn to use [code] and [/code] tags.

> Here is my code:
Yuck. Learn to use [code] and [/code] tags.

Well that is your opinion,,,

This is the way I sent it here, because of the school I am attending,,,

If you are an expert then send me a better looking code I uploaded.

Well that is your opinion,,,

This is the way I sent it here, because of the school I am attending,,,

If you are an expert then send me a better looking code I uploaded.

My opinion too, as well as most experts here. Check out this info about better formatting techniques, then post using the instructions on the background of the input box that explains CODE tags (or see this)

thanks very much for the tip,,,

been working in Visual Basic for years, not learning C++,,,

again thanks,,,,
:)

My opinion too, as well as most experts here. Check out this info about better formatting techniques, then post using the instructions on the background of the input box that explains CODE tags (or see this)

well thanks, now i have a 0 because i used your code to help write my code and im an idiot because its literally the exact same code well good job are you happy about what you've done

commented: Is this the consequences of your own actions? Also, 14 years ago! +15

Mary, I’m sorry you’re upset, but you copied code exactly you found on the Internet.

DaniWeb is designed as a learning aid to help you learn. Not to just plagiarize what has been posted.

Good luck in your studies!

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.