-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadvance_calc.cpp
More file actions
57 lines (47 loc) · 1.46 KB
/
Copy pathadvance_calc.cpp
File metadata and controls
57 lines (47 loc) · 1.46 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
#include <iostream>
using namespace std;
int main() {
double num1, num2, result;
char op;
bool first = true;
while (true) {
if (first) {
cout << "\nEnter first number: ";
while (!(cin >> num1)) {
cin.clear();
cin.ignore(1000, '\n');
cout << "Invalid number! Enter again: ";
}
first = false;
}
else {
cout << "\nPrevious result: " << num1 << "\n";
cout << "Using it as first number...\n";
}
cout << "Enter operator (+, -, *, /): ";
cin >> op;
while (op != '+' && op != '-' && op != '*' && op != '/') {
cout << "Invalid operator! Try again: ";
cin >> op;
}
cout << "Enter second number: ";
while (!(cin >> num2)) {
cin.clear();
cin.ignore(1000, '\n');
cout << "Invalid number! Enter again: ";
}
if (op == '+') result = num1 + num2;
else if (op == '-') result = num1 - num2;
else if (op == '*') result = num1 * num2;
else {
if (num2 == 0) {
cout << "Cannot divide by zero!\n";
continue;
}
result = num1 / num2;
}
cout << "Result: " << result << endl;
num1 = result; // use result for next calculation
}
return 0;
}