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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <utility>
#include <algorithm>
auto Compare(const std::string& input, std::vector<std::pair<std::string, int>>& inventory)
{
auto compareStrings = [&input](const std::pair<std::string, int>& a)
{
return a.first == input;
};
auto it = std::find_if(inventory.begin(), inventory.end(), compareStrings);
return it;
}
int main()
{
std::vector<std::pair<std::string, int>> inventory;
inventory.push_back(std::make_pair("Stimpack", 9));
inventory.push_back(std::make_pair("Pipboy", 1));
inventory.push_back(std::make_pair("10mm Pistol", 1));
inventory.push_back(std::make_pair("10mm Round", 34));
for (auto& i : inventory)
{
std::cout << i.first << '(' << i.second << ")\n";
}
std::cout << "Use what item?\n";
std::string input{ };
std::cout << '>';
std::getline(std::cin, input);
auto itr = Compare(input, inventory);
if (itr != inventory.end())
{
if (itr->first == "Pipboy")
{
std::cout << "You cannot drop " << input << " are you crazy?! Make a sensible choice this time!\n";
}
else if (itr->first == "10mm Pistol")
{
std::cout << "Shot 15 10mm rounds!\n";
auto find10mm = Compare("10mm Round", inventory);
if (find10mm != inventory.end() && find10mm->second >= 15)
{
find10mm->second -= 15;
}
}
else if (itr->first == "10mm Round")
{
std::cout << "This item can only be used in the 10mm Pistol!\n";
}
else
{
std::cout << input << " used\n";
if (itr->second > 0)
{
itr->second--;
}
}
}
else
{
std::cout << input << " does not exist\n";
}
std::cout << '\n';
for (auto& i : inventory)
{
std::cout << i.first << '(' << i.second << ")\n";
}
return 0;
}
|