darko
darko

Reputation: 2458

if statement problem?

My program is up and running but there is a problem with my if statements somewhere that is making "scalene" be the output in addition to the correct output (unless scalene is the correct output). take a look at the issue:

enter image description here

Can anyone spot the bug?

triangleShape function

# include "header.h"

triangleType triangleShape(float sideLength1, float sideLength2, float sideLength3)
{
    triangleType triangle;

    if (sideLength1 + sideLength2 < sideLength3)
        triangle = noTriangle;
    else if (sideLength1 + sideLength3 < sideLength2)
        triangle = noTriangle;
    else if (sideLength3 + sideLength2 < sideLength1)
        triangle = noTriangle;
    else if (sideLength1 == sideLength2 == sideLength3)
        triangle = equilateral;
    else if (sideLength1 == sideLength2)
        triangle = isoceles;
    else if (sideLength1 == sideLength3)
        triangle = isoceles;
    else if (sideLength2 == sideLength3)
        triangle = isoceles;
    else
        triangle = scalene;

    return triangle;
}

output function

# include "header.h"

void output (float sideLength1, float sideLength2, float sideLength3)
{
    if (triangleShape (sideLength1, sideLength2, sideLength3) == noTriangle)
        cout << "Side lenghts of " << sideLength1 << " " << sideLength2 << " and " << sideLength3 << " would not form a triangle." << endl;
    else if (triangleShape (sideLength1, sideLength2, sideLength3) == equilateral)
        cout << "A triangle with sides of lengths " << sideLength1 << " " << sideLength2 << " and " << sideLength3 << "would be " <<
        "an equilateral triangle."<< endl;
    else if (triangleShape (sideLength1, sideLength2, sideLength3) == isoceles)
        cout << "A triangle with sides of lengths " << sideLength1 << " " << sideLength2 << " and " << sideLength3 << "would be " <<
        "an isoceles triangle."<< endl;
    else (triangleShape (sideLength1, sideLength2, sideLength3) == scalene);
        cout << "A triangle with sides of lengths " << sideLength1 << " " << sideLength2 << " and " << sideLength3 << " would be " <<
        "a scalene triangle."<< endl;
}

Upvotes: 2

Views: 373

Answers (3)

Reed Copsey
Reed Copsey

Reputation: 564891

You have an extra semi-colon on this line:

 else (triangleShape (sideLength1, sideLength2, sideLength3) == scalene);

Remove the semi-colon at the end, and add an if in the line (or remove the check completely). It's causing the print following the "else" to always occur, as it becomes a separate statement.

You can do either of these:

 else if (triangleShape (sideLength1, sideLength2, sideLength3) == scalene)
    cout << "A triangle with sides of lengths " << sideLength1 << " " << sideLength2 << " and " << sideLength3 << " would be " <<
    "a scalene triangle."<< endl;

Or:

 else // There are no other options here...
    cout << "A triangle with sides of lengths " << sideLength1 << " " << sideLength2 << " and " << sideLength3 << " would be " <<
    "a scalene triangle."<< endl;

That being said, you might want to consider running your function one time and storing the results, then doing your checks against that result (or even using a switch statement).


In addition, as pointed out by Blastfurnace, your comparison is incorrect. You should use:

if ( (sideLength1 == sideLength2) && (sideLength1 == sideLength3))

Upvotes: 9

anon
anon

Reputation:

void output (float sideLength1, float sideLength2, float sideLength3)
{
    if (triangleShape (sideLength1, sideLength2, sideLength3) == noTriangle)
        cout << "Side lenghts of " << sideLength1 << " " << sideLength2 << " and " << sideLength3 << " would not form a triangle." << endl;
    else if (triangleShape (sideLength1, sideLength2, sideLength3) == equilateral)
        cout << "A triangle with sides of lengths " << sideLength1 << " " << sideLength2 << " and " << sideLength3 << "would be " <<
        "an equilateral triangle."<< endl;
    else if (triangleShape (sideLength1, sideLength2, sideLength3) == isoceles)
        cout << "A triangle with sides of lengths " << sideLength1 << " " << sideLength2 << " and " << sideLength3 << "would be " <<
        "an isoceles triangle."<< endl;
    else (triangleShape (sideLength1, sideLength2, sideLength3) == scalene);
        cout << "A triangle with sides of lengths " << sideLength1 << " " << sideLength2 << " and " << sideLength3 << " would be " <<
        "a scalene triangle."<< endl;
}

The last else should not have a semicolon at the end.

Upvotes: 0

Charles
Charles

Reputation: 11499

else (triangleShape (sideLength1, sideLength2, sideLength3) == scalene);

should be

else if (triangleShape (sideLength1, sideLength2, sideLength3) == scalene)

Upvotes: 3

Related Questions