How do I print 2 cout answers ?

Hello, how can I print two cout answers if both are correct ? I have put // near the ones that i want to appear.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  #include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
	int a,b,c,n=2;
	for(int i=1;i<=n;i++)
    {
        cout << "A="; cin >> a;
        cout << "B="; cin >> b;
        cout << "C="; cin >> c;
	if(a+b>b&&a+c>b&&b+c>a)
    {
    if(a==b && b==c) cout << "Equilateral Triangle" << endl;
    else if (a==b && b==c && a==c) cout << "Isosceles Triangle" << endl; //
    else if (a!=b && b!=c && a!=c) cout << "Right Triangle" << endl;
	else cout << "Scalene Triangle" << endl; //
    }
	else cout << "No triangles" << endl;
    }
	return 0;
}
Last edited on
I know how to do it but I can't remeber I think its
 
if(a|b > b)


also I think you would be better using a for loop but that's just my opinion, I like you am a beginner but I'm pretty sure that if you use if(var | var) it should work (shift + backslash) |
@GeneralBiiff - That's a bit-wise or, not a logical or.

The OP already has a for loop at line 8. Where do think another for loop would be appropriate?

@Arquon - How are lines 15 and 16 different? if a==b and b==c, then a==c (transitivity).
By using else if, you're making the 5 options mutually exclusive.
If you eliminate the else ifs, you make each test independent. That raises the problem of whether any triangles are found. The best way to do that is to create a flag.
1
2
3
4
5
6
7
8
9
10
  bool triangle_found = false;

  if (a == b && b == c) 
  { cout << "equilateral triangle" << endl;
     triangle_found = true;
  }
  // ... Test remaining triangle types
  if (! triangle_found) 
  { cout << "No triangles" << endl;
  }

I was doing it by my book, and it seemed to work fine, until I needed to get 2 couts to print. Also, what do I need to type to find Right Triangle ? I dont think that what i wrote is correct.
closed account (48T7M4Gy)
(a==b || a == c || b ==c) && (a != b != c) ??
That doesnt seem to work kemort.
If a=50,b=40,c=30, I should get Right Triangle and Scalene Triangle, but I get only Scalene. Right Triangle joins the group of Equilateral and Isosceles, where a=50,b=50,c=50.
closed account (48T7M4Gy)
You could be right Arquon but my 'logic' (hence 2?'s) is on the basis that an isosceles triangle has exactly 2, and only 2, equal sides. An equilateral triangle is a special case - I've never come across anybody who would describe an equilateral as an isosceles but technically we could I s'pose.)

The a=b=c string is a bit dubious and I haven't checked whether it's legal C++. That was the reason for the 2?'s. Otherwise the test should be OK for an isosceles.

The tests for the other types are different and distinct to 'my' test for isoceles.

A right triangle satisfies Pythagoras theorem.

Equilateral satisfies a=b=c

Scalene satisfies the Triangular Inequality theorem. a+b< c etc. and this test would need to be applied to all but the right triangle if I'm not mistaken.

http://mathworld.wolfram.com/TriangleInequality.html (I see now you have this anyway - good. At least you know at the outset you have a triangle of some sort. )
Last edited on
This is what I currently have:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main ()
{
    int a,b,c,n=4;
    for(int i=1;i<=n;i++)
    {
        cout << "A="; cin >> a;
        cout << "B="; cin >> b;
        cout << "C="; cin >> c;
    if(a+b>c && c+b>a && c+a>b)
    {
        if(a==b&&b==c) cout << "Equilateral" << endl;
        if(a==b||b==c||a==c) cout << "Isosceles" << endl;
        else if ((a==b || a == c || b ==c) && (a != b != c)) cout << "Right" << endl;
        else cout << "Scalene" << endl;
    }
    else cout << "No triangles" << endl;
    }
return 0;
}

What is wrong with it ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <algorithm>

void do_sort( int& a, int& b, int& c )
{
    if( a > b ) std::swap(a,b) ;
    if( b > c ) std::swap(b,c) ;
    if( a > b ) std::swap(a,b) ;
}

bool is_triangle( int a, int b, int c )
{
    if( a <= 0 || b <= 0 || c <= 0 ) return false ;
    do_sort( a, b, c ) ;
    return c < (a+b) ;
}

int count_equal( int a, int b, int c ) { return (a==b) + (b==c) + (c==a) ; }

bool is_equilateral( int a, int b, int c ) { return count_equal(a,b,c) == 3 ; }

bool is_scalene( int a, int b, int c ) { return count_equal(a,b,c) == 0 ; }

bool is_right_angled( int a, int b, int c )
{
    do_sort( a, b, c ) ;
    return ( a*a + b*b ) == (c*c) ;
}

int main ()
{
    int a, b, c ;
    std::cout << "enter the sides of an integer triangle: " ;
    std::cin >> a >> b >> c ;
    std::cout << "\n( " << a << ", " << b << ", " << c << " ): " ;

    if( is_triangle(a,b,c) )
    {
        if( is_equilateral(a,b,c) ) std::cout << "equilateral triangle\n" ;

        else if( is_scalene(a,b,c) )
        {
            std::cout << "scalene" ;
            if( is_right_angled(a,b,c) ) std::cout << ", right angled" ;
            std::cout << " triangle\n" ;
        }

        else std::cout << "isosceles triangle\n" ;
    }

    else std::cerr << "the sides do not form a (non-degenerate) triangle\n" ;
}

http://coliru.stacked-crooked.com/a/c84eae78c03f71f3
closed account (48T7M4Gy)
<= line 15 appears OK

line 16 is equilateral not isosceles

line 17 is isosceles but, on reflection it has to be extended so that only 2 equal sides can occur as well as what follows in the && ... clause.

Maybe try looking at it from this point of view
a == b != c
a == c != b
b == c != a
etc

There's no test for a right triangle as yet.

Also, I'd initialise a,b,c so you can run the tests on each easily.
Ok, thank you everyone for your help. Ill just admit that this exercise from my book is just not right. Examples might be either.
@AbstractionAnon yeah I know, I couldnt remember which was which and at that i had been at c++ only 5 weeks then... My knowledge of c++ is an apsalute minimum but i know ebough to write a small program with no graphics, was trying my best, but just remember, were all begginers at some stage.
Also would you not just use a switch?
Probably not but now that I look at it with fresh eyes, woukd it not be possible to use a switch?
Seperate one for each var and then put the values through a function that if they have a certain value they set another set of vars to a certain value which runs through a function that prints it out?

Or is what i just described what you have just done? Dont be surprised if i did...
Last edited on
would it not be possible to use a switch?

There is nothing in the OP's code that would make a reasonable switch variable.
Topic archived. No new replies allowed.