-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
43 lines (35 loc) · 1.15 KB
/
Copy pathtest.cpp
File metadata and controls
43 lines (35 loc) · 1.15 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
#include <iostream>
#include <cstring> // For strcpy
void calculator() {
char operation[10];
char input1[5], input2[5];
std::cout << "Enter operation (add, sub, mul, div): ";
std::cin >> operation;
std::cout << "Enter first number: ";
std::cin >> input1;
std::cout << "Enter second number: ";
std::cin >> input2;
char buffer[5];
strcpy(buffer, input1); // Vulnerable: No bounds checking on input1
int num1 = atoi(input1);
int num2 = atoi(input2);
if (strcmp(operation, "add") == 0) {
std::cout << "Result: " << num1 + num2 << std::endl;
} else if (strcmp(operation, "sub") == 0) {
std::cout << "Result: " << num1 - num2 << std::endl;
} else if (strcmp(operation, "mul") == 0) {
std::cout << "Result: " << num1 * num2 << std::endl;
} else if (strcmp(operation, "div") == 0) {
if (num2 != 0) {
std::cout << "Result: " << num1 / num2 << std::endl;
} else {
std::cout << "Error: Division by zero!" << std::endl;
}
} else {
std::cout << "Invalid operation!" << std::endl;
}
}
int main() {
calculator();
return 0;
}