Errors in the program to determine triangle

Asked

Viewed 374 times

0

What mistakes I’m making?

Read 3 floating point values A, B and C and order them in descending order, so that side A represents the largest of the 3 sides. Then, determine the type of triangle these three sides form, based on the following cases, always writing an appropriate message:

  1. if A B+C, display message: NO TRIANGLE

    1. if A2 = B2 + C2, display the message: RECTANGULAR TRIANGLE

    2. if A2 > B2 + C2, display the message: OBTUSE TRIANGLE

    3. if A2 < B2 + C2, display the message: ACUTANGULO TRIANGLE

    4. if the three sides are equal, display the message: EQUILATERAL TRIANGLE

    5. if only two sides are equal, display the message: ISOSCELE TRIANGLE

Code:

#include <iostream>
    using namespace std;
    int main(){

        double a=0, b=0, c=0, ValorUm=0, ValorDois=0, ValorTres=0, b2=0, a2=0, c2=0, bc=0, b2c2=0;

//aqui o usuário insere os valores...Usei as variaveis ValorUm...
//para depois inverter para a ordem "a" "b" "c" , os valores tem que
ficar  // em ordem decrecente, o valor maior tem que ser a ..por
 exemplo  //a== 10 , b == 5 , c == 3 do maior para o menor

    cin>>ValorUm>>ValorDois>>ValorTres;


 /*usei os if para inverter as posições, pelo oque testei   o problema
 nao esta nessa parte de trocar as posicoes*/

    if(ValorUm<ValorDois&&ValorDois<ValorTres)  {
        c=ValorUm;
        b=ValorDois;
        a=ValorTres;
        cout<<endl<<a<<endl<<b<<endl<<c<<endl;      
    }if(ValorUm>ValorDois&&ValorDois>ValorTres){
        a=ValorUm;
        b=ValorDois;
        c=ValorTres;
        cout<<endl<<a<<endl<<b<<endl<<c<<endl;      
    }if(ValorDois>ValorUm&&ValorTres<ValorDois&&ValorTres<ValorUm){     
        a=ValorDois;
        b=ValorUm;
        c=ValorTres;    
        cout<<endl<<a<<endl<<b<<endl<<c<<endl;      
    }if(ValorUm>ValorDois&&ValorUm<ValorTres){
        a=ValorTres;
        b=ValorUm;
        c=ValorDois;
        cout<<endl<<a<<endl<<b<<endl<<c<<endl;      
    }if(ValorUm>ValorDois&&ValorTres<ValorUm&&ValorDois<ValorTres){
        a=ValorUm;
        b=ValorTres;
        c=ValorDois;cout<<endl<<a<<endl<<b<<endl<<c<<endl;      
    }
    bc=b+c; 
    a2=a*a;
    b2=b*b;
    c2=c*c;
    b2c2=b2+c2;
    /*até onde sei é aqui , que está o problema o TRIANGULO OBTUSANGULO
    e o TRIANGULO ACUTANGULO está com diversos erros*/
//a2 a*a b2 c2...  b2c2 é b2+c2 e bc é b+c

        if (ValorUm>ValorDois+ValorTres || ValorUm==ValorDois+ValorTres){
          cout<<"NAO FORMA TRIANGULO"<<endl;

        }
        else
        if(a2==b2c2)
        {
          cout<<"TRIANGULO RETANGULO"<<endl;
        }
        if(a2>b2c2){
          cout<<"TRIANGULO OBTUSANGULO"<<endl;

        }
        if(a2 < b2c2){
            cout<<"TRIANGULO ACUTANGULO"<<endl;
     }
     if(a==c && a!=b){
         cout<<"TRIANGULO ISOSCELES"<<endl;
     }
     if(a==b && a!=c){
         cout<<"TRIANGULO ISOSCELES"<<endl;
     }
    if(a == b && a == c ){
        cout<<"TRIANGULO EQUILATERO"<<endl;
    }
    return 0;
    }
  • 1

    Your code is very polluted, it could simplify the if and Else, but that’s not the case, I ran your code and it seems all ok, what would be the error exactly?

2 answers

0


I haven’t made any improvements to your code, but I’ll give you some tips to improve it

The exit, unwanted, that you were found when the entrance was:

ValorUm=10;
ValorDois=5;
ValorTres=3

Exit:

NAO FORMA TRIANGULO                                                                                                            
TRIANGULO OBTUSANGULO  

It happened because it was necessary to define the first else with keys

#include <iostream>
using namespace std;
int main(){

        double a=0, b=0, c=0, ValorUm=0, ValorDois=0, ValorTres=0, b2=0, a2=0, c2=0, bc=0, b2c2=0;

//aqui o usuário insere os valores...Usei as variaveis ValorUm...
//para depois inverter para a ordem "a" "b" "c" , os valores tem que
//ficar   em ordem decrecente, o valor maior tem que ser a ..por
// exemplo  //a== 10 , b == 5 , c == 3 do maior para o menor

    cin>>ValorUm>>ValorDois>>ValorTres;


 /*usei os if para inverter as posições, pelo oque testei   o problema
 nao esta nessa parte de trocar as posicoes*/

    if(ValorUm<ValorDois&&ValorDois<ValorTres)  {
        c=ValorUm;
        b=ValorDois;
        a=ValorTres;
        cout<<endl<<a<<endl<<b<<endl<<c<<endl;      
    }if(ValorUm>ValorDois&&ValorDois>ValorTres){
        a=ValorUm;
        b=ValorDois;
        c=ValorTres;
        cout<<endl<<a<<endl<<b<<endl<<c<<endl;      
    }if(ValorDois>ValorUm&&ValorTres<ValorDois&&ValorTres<ValorUm){     
        a=ValorDois;
        b=ValorUm;
        c=ValorTres;    
        cout<<endl<<a<<endl<<b<<endl<<c<<endl;      
    }if(ValorUm>ValorDois&&ValorUm<ValorTres){
        a=ValorTres;
        b=ValorUm;
        c=ValorDois;
        cout<<endl<<a<<endl<<b<<endl<<c<<endl;      
    }if(ValorUm>ValorDois&&ValorTres<ValorUm&&ValorDois<ValorTres){
        a=ValorUm;
        b=ValorTres;
        c=ValorDois;cout<<endl<<a<<endl<<b<<endl<<c<<endl;      
    }
    bc=b+c; 
    a2=a*a;
    b2=b*b;
    c2=c*c;
    b2c2=b2+c2;
    /*até onde sei é aqui , que está o problema o TRIANGULO OBTUSANGULO
    e o TRIANGULO ACUTANGULO está com diversos erros*/
//a2 a*a b2 c2...  b2c2 é b2+c2 e bc é b+c

        if ((ValorUm>ValorDois+ValorTres) || (ValorUm==ValorDois+ValorTres)){
          cout<<"NAO FORMA TRIANGULO"<<endl;

        }
        else{ **// FALTAVA ESTÁ CHAVE AQUI**
        if(a2==b2c2)
        {
          cout<<"TRIANGULO RETANGULO"<<endl;
        }
        if(a2>b2c2){
          cout<<"TRIANGULO OBTUSANGULO"<<endl;

        }
        if(a2 < b2c2){
            cout<<"TRIANGULO ACUTANGULO"<<endl;
     }
     if(a==c && a!=b){
         cout<<"TRIANGULO ISOSCELES"<<endl;
     }
     if(a==b && a!=c){
         cout<<"TRIANGULO ISOSCELES"<<endl;
     }
    if(a == b && a == c ){
        cout<<"TRIANGULO EQUILATERO"<<endl;
    }
        }**// FALTAVA ESTÁ CHAVE AQUI**
    return 0;
    }

About "ACUTANGULO TRIANGLE" is a small logic error, check the condition structures if/else, for there are some if without their respective else, however not obligatory each if own their else, It’s a good practice to do it.

Remember the good programming practices of Deitel:

  1. Indent both body commands of an if/Else structure;
  2. If there are multiple levels of indentation, each level should be indented by the same additional space

TIPS

  1. Why make so many if/Else condition structures? Your code is small, so for now there will be no problems, but and in the future, what if have to implement a code with thousands of conditionals, will so many if/Else so?

Choose to do so: ( In C, because I will not give you the code ready! : P)

if (a < b){ x = a; a = b; b = x; } 
if (b < c){ x = b; b = c; c = x; }
  1. See how much cleaner your code will be using Operator ternary;
  2. When naming variables avoid names such as: bc; a2; c2; b2c2; Give meaningful names to variables.

For more questions, please answer this question: Nomenclature standard in code for C#

Source: Types of Triangles SOLUTION IN C

Types of Triangles PYTHON SOLUTION

  • What is the logical error, no error found? bc=b+c; a2=aa; B2=bb; C2=c*c; b2c2=B2+C2;

  • I made a mistake, the logic is right, the error is in the if/Else cascade, because there is if without a respective Else and in turn without key delimitation.

  • @cristovanlopes edited the answer, see if cleaned your doubts

0

#include <iostream>
using namespace std;
int main(){
    double a=0, b=0, c=0, x=0;
    cin>>a>>b>>c;
if (a < b){ x = a; a = b; b = x; } 
if (b < c){ x = b; b = c; c = x; }  
if (a < b){ x = a; a = b; b = x; }
if (a >= b + c) {cout<<"NAO FORMA TRIANGULO"<<endl;}
else
{
if(a*a==b*b+c*c){cout<<"TRIANGULO RETANGULO"<<endl;}
else 
if(a*a>b*b+c*c){cout<<"TRIANGULO OBTUSANGULO"<<endl;}
if(a*a < b*b+c*c)
{
cout<<"TRIANGULO ACUTANGULO"<<endl;
}
if(a == b && b == c )
{
cout<<"TRIANGULO EQUILATERO"<<endl;
}
else
{
    if(a==b || b==c){cout<<"TRIANGULO ISOSCELES"<<endl;}
}

 }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.