-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculator.cpp
More file actions
47 lines (45 loc) · 1.72 KB
/
calculator.cpp
File metadata and controls
47 lines (45 loc) · 1.72 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
#include <iostream>
using namespace std;
int main()
{
double num1, num2; // Declare two variables to store the numbers
char op; // Declare a variable to store the operator
double result; // Declare a variable to store the result
bool valid = true; // Declare a variable to indicate if the input is valid
cout << "Welcome to the calculator software!\n";
cout << "Enter two numbers and an operator (+, -, *, /) separated by spaces.\n";
cout << "For example: 12 + 34\n";
cout << "Enter your input: ";
cin >> num1 >> op >> num2; // Get the user's input
switch (op) // Check the operator
{
case '+': // If it is +
result = num1 + num2; // Perform addition
break;
case '-': // If it is -
result = num1 - num2; // Perform subtraction
break;
case '*': // If it is *
result = num1 * num2; // Perform multiplication
break;
case '/': // If it is /
if (num2 == 0) // Check if the second number is zero
{
cout << "Error: Cannot divide by zero.\n"; // Display an error message
valid = false; // Set the valid flag to false
}
else
{
result = num1 / num2; // Perform division
}
break;
default: // If it is anything else
cout << "Error: Invalid operator.\n"; // Display an error message
valid = false; // Set the valid flag to false
}
if (valid) // If the input is valid
{
cout << "The result is: " << result << "\n"; // Display the result
}
return 0;
}