forked from ComputerScienceSoceityNITS/Cpp-debug-24
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Functioncreate.cpp
50 lines (42 loc) · 1.12 KB
/
Functioncreate.cpp
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
#include <iostream>
#include <iomanip> // for setprecision()
#include <limits> // for numeric_limits
using namespace std;
int main() {
// Declare variables
float num1, num2, result;
char op;
// Get user input
cout << "Enter first number: ";
cin >> num1;
cout << "Enter operator (+, -, *, /): ";
cin >> op;
cout << "Enter second number: ";
cin >> num2;
// Perform calculation
switch(op) {
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
if (num2 == 0) {
cerr << "Error: division by zero\n";
return 1;
}
result = num1 / num2;
break;
default:
cerr << "Error: invalid operator\n";
return 1;
}
// Display result
cout << setprecision(numeric_limits<double>::max_digits10) // Show all significant digits
<< num1 << " " << op << " " << num2 << " = " << result << endl;
return 0;
}