Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法恢复到While循环的顶部 我对C++是全新的,对我正在处理的问题有一个问题。_C++_While Loop - Fatal编程技术网

无法恢复到While循环的顶部 我对C++是全新的,对我正在处理的问题有一个问题。

无法恢复到While循环的顶部 我对C++是全新的,对我正在处理的问题有一个问题。,c++,while-loop,C++,While Loop,我正在编写的程序旨在执行以下操作: 要求用户输入三角形三条边的长度 检查三条边是否通过三角形不等式定理(求和 任何两个边的宽度必须大于第三个边) 计算三角形的面积和周长 **在步骤2中,如果用户输入的边长违反了三角形不等式定理,则要求用户再次输入边长 例如,如果输入sides: A:7 B:16 C:10 我得到所需的输出,并被提示输入另一组边长。但是,如果我输入以下内容: A:2 B:1 C:1 然后我得到: “三角形的面积为:-1.#IND 三角形的周长为:9“ “违反了三角形不等式

我正在编写的程序旨在执行以下操作:

  • 要求用户输入三角形三条边的长度
  • 检查三条边是否通过三角形不等式定理(求和 任何两个边的宽度必须大于第三个边)
  • 计算三角形的面积和周长
  • **在步骤2中,如果用户输入的边长违反了三角形不等式定理,则要求用户再次输入边长

    例如,如果输入sides:

    • A:7
    • B:16
    • C:10
    我得到所需的输出,并被提示输入另一组边长。但是,如果我输入以下内容:

    • A:2
    • B:1
    • C:1
    然后我得到:

    “三角形的面积为:-1.#IND
    三角形的周长为: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;
    }
    }
    
    #包括“stdafx.h”
    #包括
    #包括
    #包括
    使用名称空间std;
    空隙计算(双面a、双面b、双面c);
    无效输入长度(双面和侧面a、双面和侧面b、双面和侧面c);
    bool有效(双面a、双面b、双面c);
    int _tmain(int argc,_TCHAR*argv[]
    {
    双面_a=3;
    双面_b=3;
    双面_c=3;
    while(isValid(a侧、b侧、c侧)=true)
    {
    输入长度(a侧、b侧、c侧);
    计算(a侧、b侧、c侧);
    if(isValid(a侧、b侧、c侧)=false)
    {
    
    cout让
    valid
    变量表示
    isValid(a侧、b侧、c侧)
    ,其中只能在一个位置更新状态

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

    A=2,B=1,C=1是一条长度为2的直线(退化三角形),在这里A=B+C

    当您在函数“isvalid”中测试值时:

    你不认为这种情况是退化的情况(SuthiB+SuthoC=SouthA),因此你必须用“>=”代替“>”的所有发生,如下:

    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 ;
    }
    
    int-tmain(int-argc,_-TCHAR*argv[]
    {
    双面_a=3;
    双面_b=3;
    双面_c=3;
    while(isValid(a侧、b侧、c侧)=true)
    {
    输入长度(a侧、b侧、c侧);
    if(isValid(a侧、b侧、c侧)=false)
    {
    
    你能不能a)从某个地方(或某个人,又名同学)复制粘贴这段代码,或者b)没有花太多心思。你的代码可以被解读为
    ,当侧面有效时,请求输入并计算周长,如果无效,则输出无效消息
    。相反,当用户不想退出时,它应该是
    (例如:按Esc键退出),读取输入,如果有效,则计算周长,否则输出无效消息
    。还有一些额外的抱怨:1)对于IsValid(),不要按引用传递。您实际上不想意外更改值,所以按值传递。如果类型特别大(double不是),然后通过常量引用进行传递。2)将布尔值与真或假进行比较是一件奇怪而令人困惑的事情。与其说是“if(b==true)”,不如说是“if(b)”。同样,与其说是“if(b==false)”,不如说是“if(!b)”。
    if ((side_a + side_b >= side_c) && (side_a + side_c >= side_b) && (side_b + side_c >=
        side_a))
    
    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 ;
    }