-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
172 lines (158 loc) · 4.18 KB
/
main.cpp
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
// This program allow the user to enter three numbers and it will determine what type of triangle the three side lengths would form, if any.
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;
const int W = 7;
// Prototypes
void GetData(float & N1, float & N2, float & N3);
void Validate(float a, float b, float c, bool & ErrInvalid);
void TriangleType(float a, float b, float c, char & Type, bool & RightTriangle);
void Print(float a, float b, float c, char Type, bool RightTriangle, bool ErrInvalid, bool & Repeat);
void RNG();
int main() {
// Variable Declaration
float N1, N2, N3;
char Type;
bool Repeat, ErrInvalid, RightTriangle;
Repeat = ErrInvalid = RightTriangle = false;
int random;
// User Prompt
cout << "---Welcome User---\n\n";
// Function Call
do{
GetData(N1, N2, N3);
Validate(N1, N2, N3, ErrInvalid);
if(!ErrInvalid){
TriangleType(N1, N2, N3, Type, RightTriangle);
}
Print(N1, N2, N3, Type, RightTriangle, ErrInvalid, Repeat);
} while(Repeat);
// Programming Ending
RNG();
cout << "*Sad computer noises*\n";
cout << "Goodbye... Zzz Zzz Zzz" << endl;
return 0;
}
// Function Definitions
void GetData(float & N1, float & N2, float & N3){
cout << "Enter the first length:\n";
cin >> N1;
cout << endl;
while(N1 <= 0){
cout << "ERROR: The number you entered is invalid\n";
cout << "Please enter a positive number:\n";
cin >> N1;
cout << endl;
}
cout << "Enter the second length:\n";
cin >> N2;
cout << endl;
while(N2 <= 0){
cout << "ERROR: The number you entered is invalid\n";
cout << "Please enter a positive number\n";
cin >> N2;
cout << endl;
}
cout << "Enter the third length:\n";
cin >> N3;
cout << endl;
while(N3 <= 0){
cout << "ERROR: The number you entered is invalid\n";
cout << "Please enter a positive number\n";
cin >> N3;
cout << endl;
}
}
void Validate(float a, float b, float c, bool & ErrInvalid){
// Check for Valid Triangle Lengths
if( (a > (b + c)) || (b > (a + c)) || (c > (a + b)) ){
ErrInvalid = true;
}
}
void TriangleType(float a, float b, float c, char & Type, bool & RightTriangle){
// Determine Triangle Type
if( ((a * a) + (b * b) == (c * c)) || ((b * b) + (c * c) == (a * a)) || ((c * c) + (a * a) == (b * b)) ){
RightTriangle = true; // Right Triangle
}
if( a == b && a == c && b == c){
Type = 'E'; // Equilateral Triangle
}
else if( (a != b) && (a != c) && (b !=c) ){
Type = 'S'; // Scalene Triangle
}
else{
Type = 'I'; // Isosceles Triangle
}
}
void Print(float a, float b, float c, char Type, bool RightTriangle, bool ErrInvalid, bool & Repeat){
// Local Variable Declaration
char Ans;
// User Output to console
cout << "First length: " << setw(W) << a << endl;
cout << "Second length: " << setw(W) << b << endl;
cout << "Third length: " << setw(W) << c << endl << endl;
// Print invalid triangle if invalid
if(ErrInvalid){
cout << "These side lengths can't form a valid triangle\n\n";
}
else if(Type == 'S'){
cout << "This is a Scalene ";
if(RightTriangle){
cout << "Right ";
}
}
else if(Type == 'I'){
cout << "This is an Isosceles ";
if(RightTriangle){
cout << "Right \n\n";
}
}
else if(Type == 'E'){
cout << "This is an Equilateral ";
}
cout << "Triangle \n\n";
// Determine if the user wants to run the program again
cout << "Would you like to run the program again?\n";
cout << "Enter 'Y' for yes, or 'N' for no\n";
cin >> Ans;
Ans = toupper(Ans);
while(Ans != 'Y' && Ans != 'N'){
cout << "ERROR Invalid Entry\n";
cout << "Enter 'Y' for yes, or 'N' for no\n";
cin >> Ans;
Ans = toupper(Ans);
}
if(Ans == 'Y'){
Repeat = true;
}
else{
Repeat = false;
}
cout << endl;
}
void RNG(){//prints random emoji
srand(time(NULL));
int random = (rand() % 6) + 1;
switch(random){
case 1:
cout << ";-;";
break;
case 2:
cout << "*~*";
break;
case 3:
cout << "D:";
break;
case 4:
cout << "T.T";
break;
case 5:
cout << ">.<";
break;
default:
cout << ":(";
}
cout << endl;
}