开发者

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:

if statement problem?

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 " &开发者_开发百科lt;< sideLength1 << " " << sideLength2 << " and " << sideLength3 << " would be " <<
        "a scalene triangle."<< endl;
}


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))


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

should be

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜