One of my first c++ assignments. Please take a look

Hey, I'm a brand new coder and I'm in an intro to computer science class. I have to write a code to take three sides of a triangle as input, and determine what kind of triangle it is. It should display what kind of triangle it is, show the sides, give the perimeter, and area. Also, if the numbers cannot make a triangle, it should give a report saying it's not a triangle. I've been working on it for a couple hours, and this is what i've come up with. Can someone look over it really quick and see if i'm heading in the right direction?

Thanks,
Wesley

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
 
/*
 *@file describingtriangles
 *@author Wesley Woolridge
 *@date 2013-09-23
 *Description: This program classifies triangles depending on the values of the sides you input. 
 *If the sides you input do not make a triangle, the program will give a report saying the numbers are invalid
 *Course: CS1253 Section 1
 *Logon ID: cs125387
 *Programming Project #: 2
 *Instructor: Sharma
 */
 
 #include <iostream>
 #include <cmath>
 
 using namespace std;
 
 int main()
 {
	int perimeter, side a, side b, side c, area, s;

	cout <<"Enter the first side of the triangle> ";
	cin >> side a;
	
	cout <<"Enter the second side of the triangle> ";
	cin << side b;
	
	cout <<"Enter the last side of the triangle> ";
	cin << side c;
	
	perimeter = side a + side b + side c;
	
	s = (side a+side b+side c) /2
	
	area = sqrt ( perimeter * (s - side a) * (s - side b) * (s - side c) )
	
	if (side a)^2 + (side b)^2 == (side c)^2 || (side b)^2 + (side c)^2 == (side a)^2 || (side c)^2 + (side a)^2 == (side b)^2
		cout<<"The numbers you have input make a right triangle> "end1;
		cout<<"Sides: " << side a << side b << side c << end1;
		cout<<"Perimeter: " << perimeter << end1;
		cout<<"Area: " << area << end1;

	if side a==side b || side b==side c || side a==side c
		cout<<"The numbers you have input make an isosceles triangle> "end1;
		cout<<"Sides: " << side a << side b << side c << end1;
		cout<<"Perimeter: " << perimeter << end1;
		cout<<"Area: " << area << end1;
	
	if ( side a == side b && side b == side c )
		cout<<"The numbers you have input make an equilateral triangle> " end1;
		cout<<"Sides: " << side a << side b << side c << end1;
		cout<<"Perimeter: " << perimeter << end1;
		cout<<"Area: " << area << end1;
		
	else
		cout<<"The numbers you have input make a scalene triangle> " end1;
		cout<<"Sides: " << side a << side b << side c << end1;
		cout<<"Perimeter: " << perimeter << end1;
		cout<<"Area: " << area << end1;
		
	return 0;
}
Just looking quickly, there are a few things that you need to fix.

First, ALWAYS make sure your variables have no spaces in them. Otherwise, the compiler doesn't know when the variable stops and then next part of the expression starts. For example, instead of side a, try something like side_a or sideA.

Another thing is that with your conditional statements, only the first one will be executed conditionally. C/C++ don't care about line indents, so the fact that you have indented like that will only confuse people. Instead, put the insides inside curly braces, like this:
1
2
3
4
5
6
7
8
9
if ( /* conditinal statement here*/ ) {
    // What to do
} else if ( /* conditional statement here */ ) {
    // What to do otherwise
} else if ( /* conditional statement here */ ) {
    // etc...
} else {
    // What to do if all other statements return false
}

Also notice how the conditional statement is contained within paranthesis, rather than out in the open as you had them.

Finally, where you have (side a)^2, it will not do what you think. The ^ operator is NOT the "to the power of operator", it is actually the binary XOR operator. If you don't understand that, don't worry. All you need to know is that you should do this:
1
2
3
4
// sideA^2;
pow(sideA, 2);
    // OR
sideA * sideA;
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
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
/*
 *@file describingtriangles
 *@author Wesley Woolridge
 *@date 2013-09-23
 *Description: This program classifies triangles depending on the values of the sides you input.
 *If the sides you input do not make a triangle, the program will give a report saying the numbers are invalid
 *Course: CS1253 Section 1
 *Logon ID: cs125387
 *Programming Project #: 2
 *Instructor: Sharma
 */

 #include <iostream>
 #include <cmath>

 using namespace std; // ideally, avoid this
// just write std::cout std::cin etc.

 int main()
 {
	int a, b, c ;

	cout <<"Enter the first side of the triangle> ";
	cin  >> a;

	cout <<"Enter the second side of the triangle> ";
	cin /* << */ >> b;

	cout <<"Enter the last side of the triangle> ";
	cin >> c;

    // check if a,b,c make a triangle
    {
        if( a<1 || b<1 || c<1 )
        {
            std::cerr << "not a valid triangle; can't have non-positive sides\n" ;
            return 1 ;
        }

        if( (a+b) <= c || (b+c) <= a || (c+a) <= b )
        {
            std::cerr << "not a valid triangle; sum of two sides is smaller "
                          "than the third side\n" ;
            return 1 ;
        }
    }

    // determine the type of the triangle
    {
        // a==b is a bool; true == 1 and false == 0 when converyted to an integer
        const int num_equals = (a==b) + (b==c) + (c==a) ;

        if( num_equals == 3 ) std::cout << "this is an equilateral triangle\n" ;

        else if( num_equals == 1 ) std::cout << "this is an isosceles triangle\n" ;

        else
        {
            std::cout << "this is a scalene triangle\n" ;

            // if it is scalene, it could be right-angled

            // see: http://en.wikipedia.org/wiki/Bitwise_operations_in_C#Bitwise_XOR_.22.5E.22
            const int a2 = a*a ;
            const int b2 = b*b ;
            const int c2 = c*c ;

            if( a2 == (b2+c2) || b2 == (c2+a2) || c2 == (a2+b2) )
                std::cout << "the triangle is right-angled\n" ;
        }
    }

    // calculate perimeter and area and print them out
    {
        const int perimeter = a + b + c ;
        const /*int*/ double s = perimeter / /*2*/ 2.0 ; // avoid integer division

        const /*int*/ double area = sqrt( perimeter * (s - a) * (s - b) * (s - c) ) ;

        // print them out ...
    }
}
Last edited on
@CreditClown

If sidea, sideb and sidec are the sides' lenths of the triangle then Heron's formula is:

area = sqrt ( s * (s - sidea) * (s - sideb) * (s - sidec) );

where :

s = (sidea + sideb + sidec) / 2.0
Last edited on
Thanks for the help. I'll edit it this weekend.
Topic archived. No new replies allowed.