If, if else and else problem.

Hello guys.

I have to write a program for my C++ class which tells you what kind of triangle it is after you input three sides.
I have written the program in a rough shape, it can determine what kind of triangle it is, but if I input 0 or a negative number it doesn't output the error message for user.

Any suggestions? I tried rewriting it differently, using websites and my book, but I cannot figure it out. :( Any suggestions?

Thanks!

Here is 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

int main()
{
	string threeInitials;
	double a, b, c;

	cout << "Insert your three initials. ";
	cin >> threeInitials;
	cout << "Insert length of three sides of a triangle.\n"
			"Remember that you cannot have negative numbers.\n\n";
	
	cout << "Insert A ";
	cin >> a;
	cout << "Insert B ";
	cin >> b;
	cout << "Insert C ";
	cin >> c;
	
	cout << "Your three initials are " << threeInitials << endl;
	cout << "Your three sides of triangle are " << a << ", " << b << " and " << c << endl;
	
	if (a>0 && b>0 && c>0)
	
	if ((a==b) && (b==c))
	{
		cout << "Your triangle is Equilateral.\n"
				"It is not a right triangle." << endl;
	}
	else if (a!=b && b!=c && a!=c)
	{
		cout << "Your triangle is Scalene.\n"
				"It is not a right triangle." << endl;
	}
	else if (a==b || b==c || a==c)
	{
		cout << "Your triangle is Isosceles.\n"
				"It is not a right triangle." << endl;
	}
	else if ((a*a) + (b*b) == (c*c) &&
                   (c*c) + (b*b) == (a*a) &&
                   (a*a) + (c*c) == (b*b))
	{
		cout << "Your triangle is Right-Angled.\n"
				"It is a right triangle." << endl;
	}
	else 
	{
		cout << "Your entered numbers for one or more sides lengths cannot form a triangle.\n"
				"Please review your numbers and try again." << endl;
	}

	system ("pause"); // may be necessary with Express version.

	return 0;
}
Last edited on
Added Bold Braces.Check now....
if (a>0 && b>0 && c>0){

if ((a==b) && (b==c))
{
cout << "Your triangle is Equilateral.\n"
"It is not a right triangle." << endl;
}
else if (a!=b && b!=c && a!=c)
{
cout << "Your triangle is Scalene.\n"
"It is not a right triangle." << endl;
}
else if (a==b || b==c || a==c)
{
cout << "Your triangle is Isosceles.\n"
"It is not a right triangle." << endl;
}
else if ((a*a) + (b*b) == (c*c) &&
(c*c) + (b*b) == (a*a) &&
(a*a) + (c*c) == (b*b))
{
cout << "Your triangle is Right-Angled.\n"
"It is a right triangle." << endl;
}}
Last edited on
Thank you so much!
Topic archived. No new replies allowed.