Bracket problems

I think there's a problem with my Bracket alignments or is there any problem in my if -else statement positions?


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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <iostream>
#include <cmath>
#include <iomanip>

class Triangle {
private:
	double totalAngle {}, angleA {}, angleB {}, angleC {};

public:
	Triangle(double A, double B, double C);
	void setAngles(double A, double B, double C);
	bool validateTriangle() const ;
};

Triangle::Triangle(double A, double B, double C) {
	setAngles(A, B, C);
}

void Triangle::setAngles(double A, double B, double C) {
	angleA = A;
	angleB = B;
	angleC = C;
	totalAngle = A + B + C;
}

bool Triangle::validateTriangle() const {
	return std::fabs(totalAngle - 180.0) < 0.001;
}

int main() {
	double h {}, b {}, s1 {}, s2 {}, s3 {},x {},y {},z{};

	std::cout << "\nEnter the height of the triangle : ";
	std::cin >> h;

	std::cout << "\nEnter the base of the triangle : ";
	std::cin >> b;

	const auto area { 0.5 * (h * b) };

	std::cout << std::fixed << std::setprecision(2) << "\nThe Area of the Triangle is : " << area <<"\n" ;

	std::cout << " \nEnter the first side of the triangle :  ";
	std::cin >> s1;
	std::cout << " \nEnter the second side of the triangle :  ";
	std::cin>> s2;
	std::cout <<" \nEnter the third side of the triangle :  ";
	std::cin>> s3;

	const auto perimeter {(s1 + s2 + s3)};

	std::cout << std::fixed << std::setprecision(2) << "\nThe Perimeter of the Triangle is : " <<perimeter <<"\n";


	std::cout << " \nEnter the first length of the triangle :  ";
	std::cin >> x;
	std::cout << " \nEnter the second length of the triangle :  ";
	std::cin>> y;
	std::cout <<" \nEnter the third length of the triangle :  ";
	std::cin>> z;

	if ((x <= 0) || (y <= 0) || (z <=0)){
        std:: cout <<" The inputs are invalid this is not Triangle " << "\n";
	}
	else  {
        if((x+y <=z)|| (x+z <=y)|| (y+z <=x))
            std:: cout<< "The inputs are invalid this is not Triangle " << "\n";
	}

        else if {
	    const auto longest = z;
            if (longest < x) {
                z = longest;
              const auto longest = x;
                x = z;
            }
            if (longest < y) {
                z = longest;
             const auto longest = y;
                y = z;
            }
            if( x * x + y * y == longest * longest ) {
                std::cout << "This is a right-angled triangle.\n";
            } else if( x * x + y * y > longest * longest) {
                std::cout << "This is an acute-angled triangle.\n");
            } else std::cout << "This is an obtuse-angled triangle.\n");
        }





	const Triangle set1(40, 30, 110);

	if (set1.validateTriangle())
		std::cout << " \nThe shape is a valid triangle.";
	else
		std::cout << "\nThe shape is NOT a valid triangle.";
}
Last edited on
You forgot the opening !
[code]


1
2
else if {
const auto longest = z;


The else doesn't match any preceding if! All else statements must match a preceding if statement. There can only be one else per if statement.
Last edited on
Do you mean something like:

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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <iostream>
#include <cmath>
#include <iomanip>

const double Epsilon { 0.001 };

class Triangle {
private:
	double totalAngle {}, angleA {}, angleB {}, angleC {};

public:
	Triangle(double A, double B, double C);
	void setAngles(double A, double B, double C);
	bool validateTriangle() const;
};

Triangle::Triangle(double A, double B, double C) {
	setAngles(A, B, C);
}

void Triangle::setAngles(double A, double B, double C) {
	angleA = A;
	angleB = B;
	angleC = C;
	totalAngle = A + B + C;
}

bool Triangle::validateTriangle() const {
	return std::fabs(totalAngle - 180.0) < Epsilon;
}

int main() {
	double h {}, b {}, s1 {}, s2 {}, s3 {}, x {}, y {}, z {};

	std::cout << "\nEnter the height of the triangle : ";
	std::cin >> h;

	std::cout << "\nEnter the base of the triangle : ";
	std::cin >> b;

	const auto area { 0.5 * (h * b) };

	std::cout << std::fixed << std::setprecision(2) << "\nThe Area of the Triangle is : " << area << "\n";

	std::cout << " \nEnter the first side of the triangle : ";
	std::cin >> s1;
	std::cout << " \nEnter the second side of the triangle : ";
	std::cin >> s2;
	std::cout << " \nEnter the third side of the triangle : ";
	std::cin >> s3;

	const auto perimeter { s1 + s2 + s3 };

	std::cout << std::fixed << std::setprecision(2) << "\nThe Perimeter of the Triangle is : " << perimeter << "\n";

	std::cout << " \nEnter the first length of the triangle : ";
	std::cin >> x;
	std::cout << " \nEnter the second length of the triangle : ";
	std::cin >> y;
	std::cout << " \nEnter the third length of the triangle : ";
	std::cin >> z;

	if ((x <= 0) || (y <= 0) || (z <= 0) || (x + y <= z) || (x + z <= y) || (y + z <= x))
		std::cout << "The inputs are invalid this is not a Triangle " << "\n";
	else {
		auto longest { z };

		if (longest < x) {
			//z = longest;
			longest = x;
			x = z;
		}

		if (longest < y) {
			z = longest;
			longest = y;
			y = z;
		}

		if (std::fabs(x * x + y * y - longest * longest) <= Epsilon)
			std::cout << "This is a right-angled triangle.\n";
		else if (x * x + y * y > longest * longest)
			std::cout << "This is an acute-angled triangle.\n";
		else std::cout << "This is an obtuse-angled triangle.\n";
	}

	const Triangle set1(40, 30, 110);

	if (set1.validateTriangle())
		std::cout << " \nThe shape is a valid triangle.";
	else
		std::cout << "\nThe shape is NOT a valid triangle.";
}

Last edited on
Thank you guys you solved my problem!!! very much appreciated :)
Topic archived. No new replies allowed.