-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
81 lines (63 loc) · 2.82 KB
/
main.cpp
File metadata and controls
81 lines (63 loc) · 2.82 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
// Else-if Calculator
// A simple C++ calculator i made on day 1 of my C++ journey that handles two numbers, basic arithmetic, and power operation.
// Demonstrates input validation, loops, and if-else logic.
// Compile With G++: g++ main.cpp -o main
// Compile with Clang: clang++ main.cpp -o main
// Run: ./main
#include <iostream>
#include <cmath>
#include <sstream>
using namespace std;
int main() {
double a, b;
string operation;
string input1;
string input2;
while (true) {
cout << "Enter the first number or press Q to quit. " << endl; // Get first number
cin >> input1; // Input 1
if (input1 == "Q" || input1 == "q") { // If Q or q was pressed, quit the loop
break;
}
stringstream ss1(input1); // Convert string input to double to handle numeric validation and quit option
if (!(ss1 >> a)) {
cout << "Invalid input! Please enter a number." << endl; // / Print invalid if input isn't numeric
continue; // Go to the start of the loop
}
cout << "Enter an operation (+, -, *, /, ** for power): "; // Get an operation to perform
cin >> operation; // Operation input
cout << "Enter the second number: "; // Get second number
cin >> input2; // Input 2
if (input2 == "Q" || input2 == "q") { // If Q or q was pressed, quit the loop.
break;
}
stringstream ss2(input2); // Convert string input to double to handle numeric validation and quit option
if (!(ss2 >> b)) {
cout << "Invalid input! Please enter a number." << endl; // Print invalid if input isn't numeric
continue; // Go to the start of the loop
}
double final;
if (operation == "+") { // Addition operation
final = a + b;
} else if (operation == "-") { // Subtraction operation
final = a - b;
} else if (operation == "*") { // Multiplication operation
final = a * b;
} else if (operation == "/") { // Division operation
if (b != 0) {
final = a / b;
} else {
cout << "Invalid syntax / Division by 0!" << endl; // Print cannot divide by 0.
continue; // Go to the start of the loop
}
} else if (operation == "**") {
final = pow(a,b); // Power operation (Python style operator)
} else {
cout << " Invalid operator!" << endl; // Print if operator isnt "+", "-", "*", "/" or "**"
continue; // Go to the start of the loop
}
cout << "Result: " << final << endl; // Print result
}
cout << "Loop finished!" << endl; // Finish loop message
return 0; // Return success
}