无法恢复到While循环的顶部

Unable to revert back to top of While loop

本文关键字:循环 顶部 While 恢复      更新时间:2023-10-16

我是一个c++新手,有一个关于我正在研究的问题。

我正在编写的程序被设计成做以下事情:

  1. 要求用户输入三角形的3条边的长度
  2. 检查三条边是否通过三角形不等式定理(和)的值必须大于第三边)
  3. 计算三角形的面积和周长

**在步骤2中,如果用户输入的边长违反三角形不等式定理,则要求用户再次输入边长

例如,我输入sides:

  • : 7
  • B: 16
  • C: 10

得到所需的输出,并提示输入另一组边长。但是,如果我输入以下内容:

  • : 2
  • B: 1
  • 1 <<li> C:/gh>

得到:

"三角形的面积为:-1。#印第安纳
三角形的周长为:9"

"三角不等式定理被违反了。
请再次输入A、B、C边的长度。"

给定边2,1,1,我想输出:

"三角不等式定理被违反了。
请再次输入A、B、C边的长度。"

…并返回到输入边长的提示符

我不知道我的代码哪里出错了,希望有人能帮助我。

下面是我的代码:

#include "stdafx.h"
#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;
void calculate(double& side_a, double& side_b, double& side_c);
void inputLengths(double& side_a, double& side_b, double& side_c);
bool isValid(double& side_a, double& side_b, double& side_c);
int _tmain(int argc, _TCHAR* argv[])
{
    double side_a = 3;
double side_b = 3;
double side_c = 3;
while (isValid(side_a, side_b, side_c) == true)
    {
    inputLengths(side_a, side_b, side_c);
    calculate(side_a, side_b, side_c);
    if (isValid(side_a, side_b, side_c) == false)
    {
        cout << "n";
        cout << "The triangle inequality theorem has been violated." << endl;
        cout << "Please enter the lengths of side A, side B and side C again." << endl;
        continue;
    }
}
return 0 ;
}
void calculate(double& side_a, double& side_b, double& side_c)
{
double perimeter = side_a + side_b + side_c ;
double semiperimeter = (side_a + side_b + side_c) / 2 ;
double area = sqrt(semiperimeter * (semiperimeter - side_a) * (semiperimeter -     side_b) * (semiperimeter - side_c));
    cout << "n";
    cout << "Given a triangle with the following side lengths:" << endl;
    cout << "n"; 
    cout << "Side A: " << side_a << endl;
    cout << "Side B: " << side_b << endl;
    cout << "Side C: " << side_c << endl;
    cout << "n";
    cout << "The area of the triangle is: " << area << endl;
    cout << "The perimeter of the triangle is: " << perimeter;
    cout << "n";
}
void inputLengths(double& side_a, double& side_b, double& side_c)
{
cout << "n";
cout << "Please enter the length of side A: ";
cin >> side_a;
cout << "Please enter the length of side B: ";
cin >> side_b;
cout << "Please enter the length of side C: ";
cin >> side_c;
} 
bool isValid(double& side_a, double& side_b, double& side_c)
{
// use the triangle inequality theorem to test that the sum of any two sides is
       greater than the third side
if ((side_a + side_b > side_c) && (side_a + side_c > side_b) && (side_b + side_c >
    side_a))
{
    return true;
}
else
{
    return false;
}
}

valid变量代表isValid(side_a, side_b, side_c),其中只能在一个位置更新状态。

valid = true;
while (valid == true)      // <-- `continue`s here, checking condition
{
    valid = updateState(); // i.e. dependent upon `inputLengths`
    if (valid == false)
    {
        continue;
    }
    /* implicitly */ continue;
}

这显然代表了一个问题,因为代码说"当无效时继续(或者,总是隐式地),但是只在有效时循环"。

也就是说,在无效的情况下,循环在被告知"继续"后立即结束——可以改变有效性的代码有机会再次执行

相反,请考虑以下内容:

// Loop repeatedly until the user enters a "valid input" state
valid = false;
while (true) {             
     valid = readTriangleSides(); // Ask for the input; is it valid?
     if (!valid) {
        // Inform the user the input was invalid; implicit `continue`
     } else {
        // Valid, end the loop - this could be eliminated if the
        // loop was expressed as `while (!valid) ..`
        break;
     }
}
// Use the obtained values, now in the "valid input" state
calculateAndDisplay();

A=2,B=1,C=1是一条长度为2的直线(因此是一个简并三角形),在这种情况下A=B+C

当你在函数"isvalid"中测试你的值时:

if ((side_a + side_b > side_c) && (side_a + side_c > side_b) && (side_b + side_c >
side_a))

你不认为这种情况是简并情况(其中side_b+side_c=side_a)。因此,必须用">="替换所有出现的">",如下所示:

if ((side_a + side_b >= side_c) && (side_a + side_c >= side_b) && (side_b + side_c >=
    side_a))

我看到了自己的错误。我在检查有效性之前进行计算。对calculate()的调用应该在while循环中移到较低的位置。并且,应该有一个回调inputlength_()。代码应该是:

int _tmain(int argc, _TCHAR* argv[])
{
double side_a = 3;
double side_b = 3;
double side_c = 3;
while (isValid(side_a, side_b, side_c) == true)
{
    inputLengths(side_a, side_b, side_c);

    if (isValid(side_a, side_b, side_c) == false)
    {
        cout << "n";
        cout << "The triangle inequality theorem has been violated." <<
endl;
        cout << "Please enter the lengths of side A, side B and side C
again." << endl;
        inputLengths(side_a, side_b, side_c);
    }
    calculate(side_a, side_b, side_c);
}
return 0 ;
}

感谢所有人的慷慨帮助。

,以及c .