c++ - if语句问题?

标签 c++ if-statement

我的程序已启动并正在运行,但我的 if 语句在某处存在问题,使“scalene”成为除了正确输出之外的输出(除非 scalene 是正确的输出)。看问题:

enter image description here

谁能发现这个错误?

三角形函数

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

最佳答案

你在这一行多了一个分号:

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

关于c++ - if语句问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4910815/

相关文章:

C++ - 使头文件中的结构对外部不可见

c++ - 使用 MinGW 开发 Windows DDK?

java - 使用嵌套 for 循环查找重复项

c++ - OpenCV错误: Unsupported format or combination of format - when matching ORB features with FlannBasedMatcher的解决方法

c++ - 将参数传递给 C++ 程序以在 VSCode 中进行调试

c++ - 我应该如何在 C++ 中实现对等网络?

bash - shell脚本变量值

c++ - 你如何在 C++ 中比较多个字符串

c - 无法将 12 小时格式转换为 24 小时格式

ios - 在 Swift 的 if 语句中重新分配一个值