-
Notifications
You must be signed in to change notification settings - Fork 0
/
switchStatement.cpp
99 lines (84 loc) · 2.14 KB
/
switchStatement.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
// Chapter 04, Programming Challenge 21, This programm will calculate the area
// of a circle, a rectangle or a triangle.
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;
int main()
{
// Constant
const double PI = 3.14159;
// Variables
int choice;
float radius;
float length;
float width;
float base;
float height;
float area;
// Dispaly the menu
cout << "\t Geometry Calculator \n\n"
<< "1. Calculate the Area of a Circle \n"
<< "2. Calculate the Area of a Rectangle \n"
<< "3. Calculate the Area of a Triangle \n"
<< "4. Quit \n";
cin >> choice;
// Set the numeric output formatting
cout << fixed << showpoint << setprecision(2);
// Calculate and display areas
switch (choice)
{
// Calculate circle area
case 1:
cout << "Enter the radius of the circle\n";
cin >> radius;
if (radius < 1)
cout << "Invalid input for the radius!\n";
else if (radius > 0)
{
area = PI * pow(radius, 2);
cout << "The area of the circle is: " << area << endl;
}
break;
// Calculate rectangle area
case 2:
cout << "Enter the length of the rectangle ";
cin >> length;
cout << "Enter the width of the rectangle ";
cin >> width;
if (length < 1)
cout << "Invalid input for the length!\n";
else if (width < 1)
cout << "Invalid input for the width!\n";
else if (length > 0 && width > 0)
{
area = length * width;
cout << "The area of the rectangle is: " << area << endl;
}
break;
// Calculate Triangle area
case 3:
cout << "Enter the base of the Triangle ";
cin >> height;
cout << "Enter the height of the Triangle ";
cin >> base;
if (height < 1)
cout << "Invalid input for the base!\n";
else if (base < 1)
cout << "Invalid input for the height!\n";
else if (length > 0 && width > 0)
{
area = base * height * 0.5;
cout << "The area of the triangle is: " << area << endl;
}
break;
case 4:
cout << "Program ending. \n";
break;
default:
cout << "The valid choices are 1 through 4. Restart the\n"
<< "the program and select one of those.\n";
}
return 0;
}