Not sure what are the mistakes

New to C++ here. I'm trying to write a program to determine the shape of a triangle(practicing enum).
The program runs fine and it works as intended, but I got 3 mistakes notifications on the directory list (using VS code). Also, I have two underline ~ in my code, which indicates mistakes. I have included them by adding ~ in my code. Please help and point out my mistakes(one at the last line of main, the other at the end of the code). Thanks in advance.

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 <string>
enum tri {scalene, isosceles, equilateral,noTriangle}; 
tri id (int a, int b, int c);
void order(int &a, int &b);
std::string readTri (tri ABC); 
int main ()
{
    int a, b, c; 
    std::cout << " Enter the length of sides of an triangle: " << "\n";

    std::cout << " Enter the first side: " << "\n";  

    std::cin >> a;

    std::cout << " Enter the second side: " << "\n";  

    std::cin  >> b;

    std::cout << " Enter the third side: " << "\n";  

    std::cin >> c;

    order(b,c); 
    order(a,b);
    tri ABC = id(a,b,c); 
    std::cout << " This triangle is a/an " << readTri(ABC)  ~<<~ " triangle." << "\n"; 
    

}


void order(int &a, int &b)
{
   int x; 
   if (a > b)
   {
       x = a;
       a = b; 
       b = x; 
   }
    
}

tri id (int a, int b, int c)
{
    if( a + b < c)
    {
        return noTriangle; 
    }
    else if( a == b || a == c || b == c)
    {
        if(  a == b && b == c)
        {
            return isosceles;
        }
        return equilateral; 
    }
    else 
    {
        return scalene; 
    }
}


std::string readTri (tri ABC)
{
    std::string str; 
    switch(ABC)
    {
        case 0:
        {
            str = "scalene";
            break;
        }
        case 1: 
        {
            str = "isosceles";
            break;
        }
        case 2:
        {
            str = "equilateral"; 
            break;
        }
        case 3: 
        {
            str = "noTriangle"; 
            break; 
        }
    }
    return str; 
~}~
Last edited on
Online compiler merely warns about line 35:
 In function 'void order(int&, int&)':
35:8: warning: variable 'x' set but not used [-Wunused-but-set-variable]

Why do you have a variable that you don't use?

Oh, you have logical error on lines 38-40.
The a = b; b = a; does same as a = b;
Thanks for pointing out a typo, I meant to put a, b in increasing order.
There is an error with order() - b = a??

For the switch, use the enum name rather than the number. In VS2019 this compiles with no warnings and with only a ~~ for the ABC definition as tri is an unscoped enum. Also, there is a problem with id as for 3 3 3 it's saying it's an isosceles!

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
#include <iostream>
#include <string>

enum tri { scalene, isosceles, equilateral, noTriangle };

tri id(int a, int b, int c);
void order(int& a, int& b);
std::string readTri(tri ABC);

int main()
{
	int a {}, b {}, c {};
	std::cout << " Enter the length of sides of an triangle:\n";

	std::cout << " Enter the first side: ";
	std::cin >> a;

	std::cout << " Enter the second side: ";
	std::cin >> b;

	std::cout << " Enter the third side: ";
	std::cin >> c;

	order(b, c);
	order(a, b);

	const tri ABC {id(a, b, c)};

	std::cout << " This triangle is a/an " << readTri(ABC) << " triangle.\n";
}

void order(int& a, int& b)
{
	if (a > b) {
		const int x {a};

		a = b;
		b = x;
	}
}

tri id(int a, int b, int c)
{
	if (a + b < c)
		return noTriangle;

	if (a == b || a == c || b == c) {
		if (a == b && b == c)
			return equilateral;

		return isosceles;
	}

	return scalene;
}

std::string readTri(tri ABC)
{
	std::string str;

	switch (ABC) {
		case scalene:
			str = "scalene";
			break;

		case isosceles:
			str = "isosceles";
			break;

		case equilateral:
			str = "equilateral";
			break;

		case noTriangle:
			str = "noTriangle";
			break;
	}

	return str;
}


order() could use std::swap()

1
2
3
4
5
void order(int& a, int& b)
{
	if (a > b)
		std::swap(a, b);
}

Last edited on
Thanks seeplus for suggestions on order(). I'm not sure why unscoped enum would cause any problem. Can you further explain? Also, what is the advantage of using const in
1
2
order(int &a,  int &b)
const tri ABC{id(a,b,c)}


Greatly appreciated. I'm learning.
Last edited on
unscoped enum doesn't cause a problem by itself. It's a C++ guideline that enums should be scoped - ie given a name.

Basically, when something shouldn't change then define it as const so that if it is chnaged then the compiler will report an error.
Topic archived. No new replies allowed.