blob: f82861a007fb9e283db281e7d671f4ed2a51c3ca (
plain) (
blame)
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
|
// Your Name : msglm
// Date: Feb-17-2022
// Program Title: MENU TEMPLATE with IF ELSE
// Program Description: a program with a menu structure that will allow the user to choose an option to calculate area for a: Rectangle, Circle, and a Triangle
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
using namespace std;
//Declare Functions
//I'd prefer to have these as seperate files or just declaring them at the top
void triangleArea();
void circleArea();
void rectArea();
// Named constants
int main() {
// Variable declaration
int choice;
char option;
//Program title and description for the user
cout << "MENU TEMPLATE with IF ELSE" << endl << endl;
// User input
do {
do {
cout << "Choose the corresponding number for what task you want to complete." << endl;
cout << "1 - Perform task #1 : Rectangle area" << endl;
cout << "2 - Perform task #2 : Circle area" << endl;
cout << "3 - Perform task #3 : Triangle area" << endl;
cout << "Enter selected number here: ";
cin >> choice;
if (!cin || choice<1 || choice>4) {
cout << "Invalid input selection\n\n";
//This will purge the garbage data
cin.clear(); //Clears error flag
cin.ignore(100, '\n'); //Purges 100 characters of input
}
} while (!cin || choice<1 || choice>4);
// Calculations
// Output to the screen
//This if-else statement need not exist: a switch statement would be much more optimal
if (choice == 1)
{
rectArea();
}
else if (choice == 2)
{
circleArea();
}
else if (choice == 3)
{
triangleArea();
}
else
cout << "Terminating" << endl;
cout << "Would you like to run again? (Y/N): ";
cin >> option;
cout << "\033c";
} while (option == 'Y' || option == 'y');
return 0;
}
void rectArea() {
double height;
double width;
double area;
//User Input
cout << "Perform task #1 : Rectangle area" << endl;
cout << "Input the height in cm: \n";
cin >> height;
cout << "Input the width in cm: \n";
cin >> width;
//Calculation
area = height * width;
//Output
cout << "Area of the rectangle is " << area << " cm^2 \n";
}
void circleArea() {
cout << "Perform task #2 : Circle area" << endl;
double inputRadius;
string measurement;
const double pi = 3.14159;
//User Input
cout << "Please Input the Desired Radius for your Circle in cm: ";
cin >> inputRadius;
//Output
cout << "Area of the circle is " << pi*pow(inputRadius, 2) << "cm^2 \n";
}
void triangleArea() {
double height;
double base;
//User Input
cout << "Perform task #3 : Triangle area" << endl;
cout << "Input the height in cm: \n";
cin >> height;
cout << "Input the base in cm: \n";
cin >> base;
//Output
cout << "Area of the rectangle is " << (height*base)/2 << " cm^2 \n";
}
/*This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
* You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
|