-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcalc.cpp
More file actions
96 lines (75 loc) · 2.07 KB
/
calc.cpp
File metadata and controls
96 lines (75 loc) · 2.07 KB
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
#include "calc.h"
#include <iostream>
void add(double x, double y)
{
std::cout << x << " + " << y << " = " << x + y << "\n";
}
void subtract(double x, double y)
{
std::cout << x << " - " << y << " = " << x - y << "\n";
}
void multiply(double x, double y)
{
std::cout << x << " * " << y << " = " << x * y << "\n";
}
void divide(double x, double y)
{
if (y != 0) {
std::cout << x << " / " << y << " = " << x / y << "\n";
}
else {
std::cout << "NShell encountered an error: Cannot divide by zero.\n";
}
}
void calc()
{
std::string confirm;
while (true) {
std::cout << "Please type in the first number: ";
double x{ };
std::cin >> x;
std::cout << "Please type in the second number: ";
double y{ };
std::cin >> y;
while (true) {
std::cout << "Please choose what to do.\n\n";
std::cout << "1. Add\n";
std::cout << "2. Subtract\n";
std::cout << "3. Multiply\n";
std::cout << "4. Divide\n";
std::cout << "5. Enter other numbers\n";
int sel{ };
std::cin >> sel;
switch (sel)
{
case 1:
add(x, y);
break;
case 2:
subtract(x, y);
break;
case 3:
multiply(x, y);
break;
case 4:
divide(x, y);
break;
case 5:
break;
default:
std::cout << "NShell encountered an error: Please choose a number between 1 and 5.\n";
}
std::cout << "Do you want to re-run? (Y/N): ";
std::cin >> confirm;
if (confirm == "Y" || confirm == "y" || confirm == "N" || confirm == "n") {
break;
}
else {
std::cout << "NShell encountered an error: Please type Y or N.\n";
}
}
if (confirm == "N" || confirm == "n") {
break;
}
}
}