简体   繁体   English

为什么我的代码总是跳过我的 c++ 的 else 语句?

[英]Why does my code keep skipping my else statement for c++?

I'm doing some programming exercises in a book for school and the exercise is about determining if the sides that are entered by the user indicate if it is a right-angle triangle.我在学校的书中做一些编程练习,练习是关于确定用户输入的边是否指示它是否是直角三角形。

To figure that out you have to do the Pythagorean theorem which is a^2 + b^2 = c^2.要弄清楚这一点,您必须执行毕达哥拉斯定理,即 a^2 + b^2 = c^2。 I wrote an if statement that asks if (side1 * side1) + (side2 * side2) = (side3 * side3) then it's a right triangle and if it's not I wrote an else statement to print that it's not a right triangle.我写了一个if语句,询问是否 (side1 * side1) + (side2 * side2) = (side3 * side3) 那么它是一个直角三角形,如果不是,我写了一个else语句来打印它不是一个直角三角形。 Below is the code that I wrote.下面是我写的代码。

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

int main() {
    int side1=0, side2=0, side3=0;

    cout << "Enter the first side of the triangle: " << endl;
    cin >> side1;

    cout << "Enter the second side of the triangle: " << endl;
    cin >> side2;

    cout << "Enter the third side of the triangle: " << endl;
    cin >> side3;

    if ((side1 * side1) + (side2 * side2) == side3 * side3)
    {
      cout << "It is a right angled triangle" << endl;
      exit(1);
    }

    else
    {
      cout << "It is not a right angled triangle" << endl;
      exit(1);
    }
    return 0;
}

The response that I keep getting from my code is that everything is a right triangle which is what I don't want.我不断从我的代码中得到的响应是,一切都是直角三角形,这是我不想要的。

You should sort the lengths into order as C must be the hypotenuse (the longest side) for Pythagoras' theorem to work.您应该将长度排序,因为 C 必须是勾股定理起作用的斜边(最长边)。 Collect your ints in a std::array and use std::sort, like this:在 std::array 中收集您的整数并使用 std::sort,如下所示:

#include <iostream>
#include <string>
#include <algorithm>
#include <array>

using namespace std;

int main(){
   array<int,3> side;

   cout << "Enter the first side of the triangle: " << endl;
   cin >> side[0];

   cout << "Enter the second side of the triangle: " << endl;
   cin >> side[1];

   cout << "Enter the third side of the triangle: " << endl;
   cin >> side[2];

   std::sort( begin(side), end(side) );

   if ((side[0] * side[0]) + (side[1] * side[1]) == side[2] * side[2])
   {
      cout << "It is a right angled triangle" << endl;
   }
   else
   {
      cout << "It is not a right angled triangle" << endl;
   }
   return 0;
}

Just checking that side[2] is the longest isn't sufficicent to determine whether the triangle is right-angle or not.仅检查边 [2] 是最长的不足以确定三角形是否为直角。 You need to put the longest side in the equation in the right place.您需要将等式中最长的边放在正确的位置。

Hello everyone I managed to get my code to work, I talked with my instructor this morning and she said that I was supposed to calculate for the other sides for my code to work.大家好,我设法让我的代码工作,我今天早上和我的导师谈过,她说我应该为我的代码工作的其他方面进行计算。 The code below is my fixed code that works.下面的代码是我的固定代码。

#include <iomanip>
#include <string>
#include <iostream>
#include<cmath>

using namespace std;

int main() {

int side1 = 0, side2 = 0, side3 = 0, side = 0, sidehyp = 0;

cout << "Enter the first side of the triangle: " << endl;
cin >> side1;

cout << "Enter the second side of the triangle: " << endl;
cin >> side2;

cout << "Enter the third side of the triangle: " << endl;
cin >> side3;

side = (side1*side1) + (side2*side2);
    sidehyp = (side3*side3);


    if (side1 * side1 + side2 * side2 == side3 * side3)
    {
    cout << "It is a right angled triangle" << endl;
    }

    else if (side2 * side2 + side3 * side3 == side1 * side1)
    {
        cout << "It is a right angled triangle" << endl;
    }

    else if (side3 * side3 + side1 * side1 == side2 * side2)
    {
        cout << "It is a right angled triangle" << endl;
    }

    else
    {
        cout << "It is not a right angled triangle" << endl;
    }
return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM