如果声明问题?

时间:2011-02-06 01:02:26

标签: c++ if-statement

我的程序启动并运行但是我的if语句出现问题,除了正确的输出之外,“scalene”是输出(除非scalene是正确的输出)。看一下这个问题:

enter image description here

有人能发现这个错误吗?

triangleShape函数

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

输出功能

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

3 个答案:

答案 0 :(得分:9)

这一行你有一个额外的分号:

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

删除末尾的分号,并在行中添加if(或完全删除检查)。它导致“else”之后的打印始终发生,因为它变成了一个单独的声明。

您可以执行以下任一操作:

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

或者:

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

话虽这么说,您可能需要考虑一次运行您的函数并存储结果,然后根据该结果进行检查(甚至使用switch statement)。


此外,正如Blastfurnace所指出的,您的比较是不正确的。你应该使用:

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

答案 1 :(得分:3)

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

应该是

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

答案 2 :(得分:0)

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

最后一个不应该在结尾处有分号。