fork download
  1. #include <iostream>
  2. #include <map>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. map<string, int> inventory;
  8.  
  9. void addItem(string name, int quantity) {
  10. inventory[name] += quantity;
  11. }
  12.  
  13. void removeItem(string name, int quantity) {
  14. inventory[name] -= quantity;
  15. if (inventory[name] <= 0) {
  16. inventory.erase(name);
  17. }
  18. }
  19.  
  20. int checkQuantity(string name) {
  21. if (inventory.count(name) == 0) {
  22. return 0;
  23. }
  24. return inventory[name];
  25. }
  26.  
  27. void displayInventory() {
  28. cout << "Total Inventory:" << endl;
  29. for (auto item : inventory) {
  30. cout << item.first << ": " << item.second << endl;
  31. }
  32. }
  33.  
  34. int main() {
  35.  
  36. addItem("Apples", 10);
  37. addItem("Oranges", 15);
  38.  
  39. removeItem("Apples", 5);
  40.  
  41. cout << "Apples: " << checkQuantity("Apples") << endl;
  42.  
  43. displayInventory();
  44.  
  45. return 0;
  46. }
Success #stdin #stdout 0s 5308KB
stdin
Standard input is empty
stdout
Apples: 5
Total Inventory:
Apples: 5
Oranges: 15