-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
58 lines (47 loc) · 2.04 KB
/
main.cpp
File metadata and controls
58 lines (47 loc) · 2.04 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
#include <iostream>
#include <limits> // Input validation
int main() {
std::cout << "\nWelcome, Adventurer!\n";
std::cout << "\nYou've stumbled upon a magical WEAPONS CHEST!\n";
std::cout << "\nInput an elemental strength value for each weapon...\n";
int fireStrength = 0, sedativeStrength = 0, destructionStrength = 0;
std::cout << "\nEnter the Fire Value for the Flaming Sword: \n";
if (!(std::cin >> fireStrength)) {
std::cerr << "Invalid input. Only whole numbers allowed!\n";
return 1;
}
std::cout << "\nEnter Sedative Value for the Snooze Bow: \n";
if (!(std::cin >> sedativeStrength)) {
std::cerr << "Invalid input. Only whole numbers allowed!\n";
return 1;
}
std::cout << "\nEnter Destruction Value for the Felling Axe: \n";
if (!(std::cin >> destructionStrength)) {
std::cerr << "Invalid input. Only whole numbers allowed!\n";
return 1;
}
// Secure dynamic memory allocation
int* ptrFire = nullptr;
int* ptrSedative = nullptr;
int* ptrDestruction = nullptr;
try {
ptrFire = new int(fireStrength);
ptrSedative = new int(sedativeStrength);
ptrDestruction = new int(destructionStrength);
} catch (const std::bad_alloc& e) {
std::cerr << "Memory allocation failed: " << e.what() << "\n";
delete ptrFire; delete ptrSedative; delete ptrDestruction;
return 1;
}
std::cout << "\n Weapons Chest has been activated!\n\n";
std::cout << "Flaming Sword / Fire Value = " << *ptrFire << "\n";
std::cout << "Snooze Bow / Sedative Value = " << *ptrSedative << "\n";
std::cout << "Felling Axe / Destruction Value = " << *ptrDestruction << "\n";
std::cout << "\n All weapons equipped and ready for the fight!\n";
// Clean up memory
delete ptrFire; ptrFire = nullptr;
delete ptrSedative; ptrSedative = nullptr;
delete ptrDestruction; ptrDestruction = nullptr;
std::cout << "\n ...Fight concluded. Weapons Chest locked until your next adventure!\n\n";
return 0;
}