-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic template sample.cpp
55 lines (53 loc) · 1.07 KB
/
basic template sample.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
51
52
53
54
55
#include <iostream>
using namespace std;
template<typename T>
T performOperation(T a, T b, char op)
{
T result;
switch (op)
{
case '+':
result = a + b;
break;
case '-':
result = a - b;
break;
case '*':
result = a * b;
break;
case '/':
if (b == 0) {
cout << "Error: Division by zero" << endl;
result = 0;
}
else {
result = a / b;
}
break;
default:
cout << "Error: Invalid operation" << endl;
result = 0;
break;
}
return result;
}
int main()
{
int a, b; // this can be float, int or double too
char op;
cout << "Enter first operand ";
cin >>a;
cout<<"Enter second operand ";
cin>>b;
cout<<"Enter operation";
cin>>op; // op can be +, -, * or /
if (op == '*'|| op == '+'|| op == '-' || op == '/')
{
cout<< performOperation(a, b, op);
}
else
{
cout << "Wrong operation";
}
return 0;
}