if else if and bools problem D:

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
53
54
55
56
/*
4-70 Triangles 
Write a program that will read three numbers (representing the lengths of the sides of a triangle) 
and print out one of the following four words:

NONE if the three lengths do not represent the sides of a triangle 
(i.e., if the sum of the lengths of any two sides is not greater than the third side)

GENERAL if the lengths specify a general triangle 
(i.e., one whose sides are all of different lengths)

ISOSCELES if any two sides are of equal length

EQUILATERAL if all three sides of the triangle are of equal length

Write a program so that it will print out the lengths of the three sides 
along with the type of triangle they represent. The program should process an arbitrary 
number of sets of three numbers and will terminate upon reading a set of three numbers all of which are zero.
*/
#include <iostream>
using namespace std;
int main ()
{
 bool yes = false;
 do {
 double a, b, c;
 cout << "Tell me any three lengths and I will tell you what type of triangle it is.\n";
 cout << "What is side a: \n";
 cin >> a;
 cout << "What is side b: \n";
 cin >> b;
 cout << "What is side c: \n";
 cin >> c;

 if ( a=b=c=0)
 {cout << "All three cannot be zero!\n";}

 else if ( a+b<c || b+c<a || a+c<b)
 {cout << "Not a triangle! Try again!\n";
 cout << "Side A is: " << "B is: " << "C is: " << endl;}
 else if (a != b != c)
 {cout << "This is a general triangle!\n";
 cout << "Side A is: " << "B is: " << "C is: " << endl;}
 else if (a=b || b=c || a=c)
 {cout << "This is an isosceles triangle!\n";
 cout << "Side A is: " << "B is: " << "C is: " << endl;}
 else (a=b=c)
 {cout << "This is an equilteral triangle!\n";
 cout << "Side A is: " << "B is: " << "C is: " << endl;}

 cout << "Do you want to continue (yes/no): \n";
} while (!yes);

cout << "Goodbye!\n";
system ("pause");
}


Heey guys! this is my code. i was wondering why i get this error 1>triangle.cpp(44): error C2106: '=' : left operand must be l-value at line 44

and 1>triangle.cpp(44): warning C4800: 'double' : forcing value to bool 'true' or 'false' (performance warning)

and 1>triangle.cpp(41): warning C4805: '!=' : unsafe mix of type 'bool' and type 'double' in operation those errors! any help would be greatly appreciated! D:
To check if two values are the same use double equals, == instead of =
you need to use the == to see if it is equal.

example
1
2
3
4
5
6
if (a==b&&b==c&&c==0)
{

cout << "All three cannot be zero!\n";

}
Last edited on
ohhkay!thanks guys! i've edited my code to this
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
#include <iostream>
using namespace std;
int main ()
{
 bool no = false;
 do {
 double a=0, b=0, c=0;
 cout << "Tell me any three lengths and I will tell you what type of triangle it is.\n";
 cout << "What is side a: \n";
 cin >> a;
 cout << "What is side b: \n";
 cin >> b;
 cout << "What is side c: \n";
 cin >> c;

 if ( a==b&&b==c&&c==0)
 {cout << "All three cannot be zero!\n";}
 else if ( a+b<c || b+c<a || a+c<b)
 {cout << "Not a triangle! Try again!\n";
 cout << "Side A is: " << a << " B is: " << b << " C is: " << c << endl;}
 else if (a != b != c)
 {cout << "This is a general triangle!\n";
  cout << "Side A is: " << a << " B is: " << b << " C is: " << c << endl;}
 else if (a==b || b==c || a==c)
 {cout << "This is an isosceles triangle!\n";
 cout << "Side A is: " << a << " B is: " << b << " C is: " << c << endl;}
 else (a==b&&b==c&&c==a);
 {cout << "This is an equilteral triangle!\n";
  cout << "Side A is: " << a << " B is: " << b << " C is: " << c << endl;}
 
 cout << "Do you want to try more? (yes/no): \n";
} while (!no);

cout << "Goodbye!\n";
system ("pause");
}


i think something is wrong with my do while loop :\ because i cant type yes or no. it just repeats! gaah!

and also, i tried plugging in a as 10, b as 10, and c as 15, but it tells me that its both equilateral and isosceles, when its supposed to be an isosceles. does anyone see my error?
Just like you did for a,b and c, you need to declare a variable such as char again and ask the user input "y" for yes and "n" for no. Once you've done that you need to make an if statement and check if(again == 'y') etc... just like you did with a,b and c.

Post back if you need more help.
Last edited on
This might help.
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
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <limits>
using namespace std;
int main ()
{
 bool no = false;
 char again;
 do {
 double a=0, b=0, c=0;
 cout << "Tell me any three lengths and I will tell you what type of triangle it is.\n";
 cout << "What is side a: \n";
 cin >> a;
 cout << "What is side b: \n";
 cin >> b;
 cout << "What is side c: \n";
 cin >> c;

 if ( a==b&&b==c&&c==0)
 {
	 cout << "All three cannot be zero!\n";
 
 }
 else if ( (a+b)<c || (b+c)<a || (a+c)<b)
 {
 cout << "Not a triangle! Try again!\n";
 cout << "Side A is: " << a << " B is: " << b << " C is: " << c << endl;
 
 }
 else if (a != b && b != c)
 {
  cout << "This is a general triangle!\n";
  cout << "Side A is: " << a << " B is: " << b << " C is: " << c << endl;
 }
 
 else if (a==b || b==c || a==c)
 {
 cout << "This is an isosceles triangle!\n";
 cout << "Side A is: " << a << " B is: " << b << " C is: " << c << endl;
 }
 else if (a==b&&b==c&&c==a)
 {
  cout << "This is an equilteral triangle!\n";
  cout << "Side A is: " << a << " B is: " << b << " C is: " << c << endl;
 }
 
 cout << "Do you want to try more? ('y' for yes/'n' for no): \n";
 cin>> again;
 if (again=='y')
	no = false;
 else
	 no=true;
} while (!no);

cout << "Goodbye!\n";
cin.sync();
cout<<"Press Enter Key To Exit"<<endl;
cin.ignore( numeric_limits<streamsize>::max(),'\n' );
}







Krahl (42) Mar 7, 2012 at 7:53pm
Just like you did for a,b and c, you need to declare a variable such as char again and ask the user input "y" for yes and "n" for no. Once you've done that you need to make an if statement and check if(again == 'y') etc... just like you did with a,b and c.

Post back if you need more help.


okaay! i'm going blindly now~! i'm not sure what i did LOOL..
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
#include <iostream>
using namespace std;
int main ()
{
 char again;
 bool y = true;
 do {
 double a=0, b=0, c=0;
  if (again == y)
 {cout << "Tell me any three lengths and I will tell you what type of triangle it is.\n";
 cout << "What is side a: \n";
 cin >> a;
 cout << "What is side b: \n";
 cin >> b;
 cout << "What is side c: \n";
 cin >> c;

 if ( a==b&&b==c&&c==0)
 {cout << "All three cannot be zero!\n";}
 else if ( a+b<c || b+c<a || a+c<b)
 {cout << "Not a triangle! Try again!\n";
 cout << "Side A is: " << a << " B is: " << b << " C is: " << c << endl;}
 else if (a != b != c)
 {cout << "This is a general triangle!\n";
  cout << "Side A is: " << a << " B is: " << b << " C is: " << c << endl;}
 else if (a==b || b==c || a==c)
 {cout << "This is an isosceles triangle!\n";
 cout << "Side A is: " << a << " B is: " << b << " C is: " << c << endl;}
 else (a==b&&b==c&&c==a);
 {cout << "This is an equilteral triangle!\n";
  cout << "Side A is: " << a << " B is: " << b << " C is: " << c << endl;}
 
 cout << "Do you want to try more? (yes/no): \n";
 cin >> again;
} while (!y);}

 else (again == n)
{cout << "Goodbye!\n";
system ("pause");}
}
Ok go back to your previous program and put
1
2
3
4
if (again == 'n')
no = true;
}while(!no);
right after line 34.

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
#include <iostream>
using namespace std;
int main ()
{
 char again;
 bool no = true;
 do {
 double a=0, b=0, c=0;
 cout << "Tell me any three lengths and I will tell you what type of triangle it is.\n";
 cout << "What is side a: \n";
 cin >> a;
 cout << "What is side b: \n";
 cin >> b;
 cout << "What is side c: \n";
 cin >> c;

 if ( a==b&&b==c&&c==0)
 {cout << "All three cannot be zero!\n";}
 else if ( a+b<c || b+c<a || a+c<b)
 {cout << "Not a triangle! Try again!\n";
 cout << "Side A is: " << a << " B is: " << b << " C is: " << c << endl;}
 else if (a != b && a != c && b != c)
 {cout << "This is a general triangle!\n";
  cout << "Side A is: " << a << " B is: " << b << " C is: " << c << endl;}
 else if (a==b || b==c || a==c)
 {cout << "This is an isosceles triangle!\n";
 cout << "Side A is: " << a << " B is: " << b << " C is: " << c << endl;}
 else (a==b&&b==c&&c==a);
 {cout << "This is an equilteral triangle!\n";
  cout << "Side A is: " << a << " B is: " << b << " C is: " << c << endl;}
 
 cout << "Do you want to try more? (y/n): \n";
 cin >> again;
 if(again == 'n')
	 no = true;
} while (!no);


cout << "Goodbye!\n";
system ("pause");
return 0;
}


The reason why you weren't getting isosceles was because of the following line
else if (a != b != c)
Last edited on

Krahl (46) Mar 7, 2012 at 8:31pm
Ok go back to your previous program and put

if (again == 'n')
no = true;
}while(!no);
right after line 34.


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
#include <iostream>
using namespace std;
int main ()
{
 char again, n;
 do {
 double a=0, b=0, c=0;
 cout << "Tell me any three lengths and I will tell you what type of triangle it is.\n";
 cout << "What is side a: \n";
 cin >> a;
 cout << "What is side b: \n";
 cin >> b;
 cout << "What is side c: \n";
 cin >> c;

 if ( a==b&&b==c&&c==0)
 {cout << "All three cannot be zero!\n";}
 else if ( a+b<c || b+c<a || a+c<b)
 {cout << "Not a triangle! Try again!\n";
 cout << "Side A is: " << a << " B is: " << b << " C is: " << c << endl;}
 else if (a != b != c)
 {cout << "This is a general triangle!\n";
  cout << "Side A is: " << a << " B is: " << b << " C is: " << c << endl;}
 else if (a==b || b==c || a==c)
 {cout << "This is an isosceles triangle!\n";
 cout << "Side A is: " << a << " B is: " << b << " C is: " << c << endl;}
 else (a==b&&b==c&&c==a);
 {cout << "This is an equilteral triangle!\n";
  cout << "Side A is: " << a << " B is: " << b << " C is: " << c << endl;}
 
 cout << "Do you want to try more? (yes/no): \n";
 cin >> again;
 if (again == 'n')
n = true;
}while(!n);

 cout << "Goodbye!\n";
system ("pause");
}



WOOOOW! THANK YOU KRAHL! IT WORKS NOW :D YOU ROCK!!!
ohh :\ i realized my other problem. after i enter my sides, the program keeps including the last else!

i tried changing the last else to an else if so here's my code
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
#include <iostream>
using namespace std;
int main ()
{
 char again, n;
 do {
 double a=0, b=0, c=0;
 cout << "Tell me any three lengths and I will tell you what type of triangle it is.\n";
 cout << "What is side a: \n";
 cin >> a;
 cout << "What is side b: \n";
 cin >> b;
 cout << "What is side c: \n";
 cin >> c;

 if ( a==b&&b==c&&c==0)
 {cout << "All three cannot be zero!\n";}
 else if ( a+b<=c || b+c<=a || a+c<=b)
 {cout << "Not a triangle! Try again!\n";
 cout << "Side A is: " << a << " B is: " << b << " C is: " << c << endl;}
 else if (a != b != c)
 {cout << "This is a general triangle!\n";
  cout << "Side A is: " << a << " B is: " << b << " C is: " << c << endl;}
 else if (a==b || b==c || a==c)
 {cout << "This is an isosceles triangle!\n";
 cout << "Side A is: " << a << " B is: " << b << " C is: " << c << endl;}
 else if (a==b&&b==c&&c==a)
 {cout << "This is an equilteral triangle!\n";
  cout << "Side A is: " << a << " B is: " << b << " C is: " << c << endl;}
 
 cout << "Do you want to try more? (yes/no): \n";
 cin >> again;
 if (again == 'n')
n = true;
}while(!n);

 cout << "Goodbye!\n";
system ("pause");
}


but now i cant get an equilateral triangle. if i put all 3 sides the same, it still calls it a general triangle!
Did you change line 21 to what I said?
Also you need to check for an equilateral triangle before you check for and isosceles triangle.
Last edited on
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
#include <iostream>
using namespace std;
int main ()
{
 char again, n;
 do {
 double a=0, b=0, c=0;
 cout << "Tell me any three lengths and I will tell you what type of triangle it is.\n";
 cout << "What is side a: \n";
 cin >> a;
 cout << "What is side b: \n";
 cin >> b;
 cout << "What is side c: \n";
 cin >> c;

 if ( a==b&&b==c&&c==0)
 {cout << "All three cannot be zero!\n";}
 else if ( a+b<=c || b+c<=a || a+c<=b)
 {cout << "Not a triangle! Try again!\n";
 cout << "Side A is: " << a << " B is: " << b << " C is: " << c << endl;}
 else if (a != b && a != c && b !=c)
 {cout << "This is a general triangle!\n";
  cout << "Side A is: " << a << " B is: " << b << " C is: " << c << endl;}
 else if (a==b&&b==c&&c==a)
 {cout << "This is an equilteral triangle!\n";
  cout << "Side A is: " << a << " B is: " << b << " C is: " << c << endl;}
 else if (a==b || b==c || a==c)
 {cout << "This is an isosceles triangle!\n";
 cout << "Side A is: " << a << " B is: " << b << " C is: " << c << endl;}
 
 
 cout << "Do you want to try more? (yes/no): \n";
 cin >> again;
 if (again == 'n')
n = true;
}while(!n);

 cout << "Goodbye!\n";
system ("pause");
}
OMMGGGG KRAHL! thank you so much. sorry ! i didnt see the last comment about line 21. YAAAY! :D THIS project took so long >.<


OHH! one last question: how would i terminate the program if they all equaled 0? (in the first if)
This line:
else if (a != b != c)
is not what you would expect it to be...(I can't think of an easy way off the top of my head to explain what this means).

It should be
else if (a != b && a != c && b != c)

Also, you should probably put the check for an equilateral triangle above the one for an isosceles triangle, or change the condition for the isosceles triangle to make sure the three sides aren't equal (because if they are, then it's equilateral).
(Easier to just move the check for an equilateral triangle above the one for an isosceles one)

Oh, and just so you understand, the reason your last else (in your previous code) wasn't working was because your code was the same as this:
1
2
3
4
5
6
7
8
9
else
{
    (a==b && b==c && c==a); // This statement does nothing
}
{ // This set of brackets doesn't really do anything in this case, so
    // this is always displayed
    cout << "This is an equilteral triangle!\n";
    cout << "Side A is: " << a << " B is: " << b << " C is: " << c << endl;
}


EDIT: Wow, you guys beat me to it...
Last edited on
Nice. Well there are many ways you could do it but I guess the simplest way is to put

1
2
3
cout << "Goodbye!\n";
system ("pause");
return 0;


wherever you want the program to terminate.
To terminate the program when they all equal 0, you just need this...
1
2
3
4
5
if (a==b && b==c && c==0)
{
    cout << "All three cannot be zero!\n";
    break; // This gets the program out of your do-while loop, so it prints "Goodbye!"
}
OHH wow! thanks long double main! totally forgot about the breaks ! THANKS EVERYONE WHO HELPED ME!
Topic archived. No new replies allowed.