C++ Triangle Solving Program

I am trying to write a program that finds the missing parts of a triangle. It doesn't work with all of the triangles I've tried and can't figure it out.

Thank you in advance for any help.
Any questions please email me at <zaricpp@gmail.com>

I do intend for this to be open-source software when It is finished.

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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
//Version 0.9


/* Note, a capital letter represents an angle measure and
 the same letter lowercase represents the length of the side opposite to that angle */

#include <iostream>
#include <cmath>
#define pi 3.14159265358989

using namespace std;

double A = 0;
double a = 0;
double B = 0;
double b = 0;
double C = 0;
double c = 0;
double areavar;
double area;


void degrad();//Converts Degrees to Radians
void raddeg();//Converts Radians to Degrees
void missang();//Finds the missing angle if two are known in a triangle
void los();//Law of Sines
void loc();//Law of Cosines
void printhead();//Prints the title screen
void angcheck();//Checks for an angle >180


int main()
{
    printhead();

    cout << "Hello, This program will solve a triangle given certain information." << endl;
    cout << "Note, It does not have to be a right triangle." << endl;
    system("pause");
    system("cls");
    cout << "Enter any information that you know (Angles in degrees) (If you dont, enter 0)" << endl;
    system("pause");
    system("cls");

    //Enter known parts
    cout << "m<A = "; cin >> A;
    cout << "a = "; cin >> a;
    cout << "m<B = "; cin >> B;
    cout << "b = "; cin >> b;
    cout << "m<C = "; cin >> C;
    cout << "c = "; cin >> c;

    angcheck();

    missang();
    degrad();


    for (int i = 1; i != 10; i++)
    {

        los();
        raddeg();
        missang();
        degrad();
        loc();
        raddeg();
        missang();
        degrad();
    }

    raddeg();


    system("cls");
    cout << "The Solution of the triangle is:" << endl << endl;
    cout << "m<A = " << A << endl;
    cout << "a = " << a << endl;
    cout << "m<B = " << B << endl;
    cout << "b = " << b << endl;
    cout << "m<C = " << C << endl;
    cout << "c = " << c << endl << endl;



    area = 1/2 * a * b * sin(C);

    cout << "The area is " << area << endl;
    cout << "The perimiter is " << a + b + c << endl << endl;

    system("pause");




    return 0;
}

void degrad()
{
    //Converts degrees to radians
    A = A * (pi / 180);
    B = B * (pi / 180);
    C = C * (pi / 180);
}

void raddeg()
{
    //Converts radians back to degrees for final answer
    A = A * (180 / pi);
    B = B * (180 / pi);
    C = C * (180 / pi);
}

void los()
//Uses Law of Sines
{
    for (int i = 1; i <= 10; i++)
    {
        //solves for a
        if (C != 0 && c != 0 && A != 0)
        {
                a = (sin(A) * c) / sin(C);
        }



        //Solves for b
        else if (A != 0 && a != 0 && B != 0)
        {
                b = (sin(B) * a) / sin(A);
        }


        //Solves for c
        else if (A != 0 && a != 0 && C!= 0)
        {
                c = (sin(C) * a) / sin(A);
        }

        //Solves for A
        else if (C != 0 && c != 0 && a != 0)
        {
                A = asin((a * sin(B) / b));
        }

        //Solves for B
        else if (A != 0 && a != 0 && B != 0)
        {
                B = asin((b * sin(A) / a));
        }

        //Solves for C
        else if (B != 0 && b != 0 && c != 0)
        {
                C = asin((c * sin(B) / b));
        }

        else if (A != 0 && a != 0 && B != 0 && b != 0 && C != 0 && c != 0)
        {
            break;
        }
    }
}

void missang()
//Finds the missing angle if only 2 are entered
{
    //Finds A
    if (A == 0 && B != 0 && C != 0)
    {
        A = 180 - C - B;
    }

    //Finds B
    else if (A != 0 && B == 0 && C !=0)
    {
        B = 180 - A - C;
    }

    //Finds C
    else if (A != 0 && B != 0 && C == 0)
    {
        C = 180 - A - B;
    }

}

void loc()
//Uses Law of cosines.
{
    for (int i = 1; 1 <= 10; i++)
    {
        //Solves for a
        if (A != 0 && b != 0 && c != 0)
        {
            a = sqrt( b*b + c*c -2*b*c*cos(A));
        }

        //Solves for b
        else if (B != 0 && a != 0 && c != 0)
        {
            b = sqrt( a*a + c*c - 2*a*c*cos(B));
        }

        //Solves for c
        else if (C != 0 && a != 0 && b !=0)
        {
            c = sqrt( a*a + b*b - 2*a*b*cos(C));
        }

        //Solves for A FIX
        else if (a != 0 && b != 0 && c != 0 && A == 0)
        {
            A = acos((b*b + c*c - a*a) / 2*b*c);
        }

        //Solves for B
        else if (a != 0 && b != 0 && c != 0 && B == 0)
        {
            B = acos((a*a + c*c - b*b) / 2*a*c);
        }

        //Solves for C
        else if (a != 0 && b != 0 && c != 0 && C == 0)
        {
            C = acos((a*a + b*b - c*c) / 2*a*b);
        }
    }
}

void printhead()
{
    cout << " -------------------------------------------------------------" << endl;
    cout << "                  Welcome to Triangle Solver                  "   << endl;
    cout << " -------------------------------------------------------------" << endl << endl;
}

void angcheck()
{
    if (A >= 180 || B >= 180 || C >= 180)
    {
        cout << "Error, one or more angle(s) is/are too large. Please try again." << endl;
        system("pause");
    }
}
Last edited on
Not trying to be mean, but it's all great that you want to open source this and stuff, but I highly doubt anyone would want to use this. Excessive use of system calls and global variables make this code not really reusable, and the use of a triangle solver is sort of limited to begin with (with trigonometry being a very elementary branch of maths that basically everyone knows to a degree and stuff).

But let's just ignore this for now:

angcheck should also check the sum of A, B and C - it may not be higher than 180 either (with it being as it is now, 179 179 179 would be valid angles in your program).

in los, the else ifs should be ifs only - the laws aren't mutually exclusive... and other stuff like that.

But really, you should reconsider the original design- it would be far more useful if you would write this as a library instead of a program.





Thank you. I do apprecate your time. I have only been doing C++ for about 2 months now and I am trying to learn. Exactly why are system calls and global variables bad?
Your system calls make this program run only on dos/windows, and they are completely unnecessary. Aside from that, they are also pretty inefficient, though speed is probably something you don't need to worry about in such a small program.

Global variables are, in this context, sort of okay on the program level. However, you can't use any of these functions outside this one program because you rely on global variables to be present. Also, your functions don't really do what they say they do - they don't really apply the sine or cosine laws, you have to call them in a specific order with other functions for them to do what they should do.
Topic archived. No new replies allowed.