Menu
Coddy logo textTech

Removing Stock

Part of the Logic & Flow section of Coddy's C++ journey — lesson 33 of 56.

challenge icon

Challenge

Easy

Complete your inventory tracking system by implementing the functionality to remove stock from existing items. This lesson demonstrates how to safely decrease inventory quantities while preventing stock levels from going below zero and handling items that don't exist in the inventory.

Start with the following initial inventory:

  • apples: 50
  • bananas: 30
  • oranges: 25

The following inputs will be provided:

  • An integer n representing the number of stock removal operations to perform
  • Then n pairs of inputs:
    • A string representing the item name
    • An integer representing the quantity to remove

Your program should:

  1. Start with the initial inventory listed above
  2. Read the number of stock removal operations
  3. For each operation, read an item name and quantity to remove
  4. Use the .count() method to check if the item exists in the inventory map
  5. If the item doesn't exist, print an error message
  6. If the item exists, check if removing the quantity would make the stock negative
  7. If the removal would result in negative stock, print an error message
  8. If the removal is valid, subtract the quantity from the current stock and print a confirmation message
  9. After all operations, print the final inventory showing all items and their updated quantities

Use the following exact output format:

For successful stock removal:

Removed [quantity] [item_name]. New total: [new_total]

For items not found:

Error: [item_name] not found in inventory

For insufficient stock:

Error: Cannot remove [quantity] [item_name]. Only [current_stock] in stock

Final inventory:

Final Inventory:\n[item1]: [quantity1]\n[item2]: [quantity2]\n[item3]: [quantity3]\n...

The final inventory should be printed in alphabetical order by item name. Use inventory.count(itemName) to check if an item exists before attempting to remove stock. Only subtract from the inventory if the item exists and the current stock is greater than or equal to the quantity being removed. Use inventory[itemName] -= quantity to subtract the quantity from existing stock.

Try it yourself

#include <iostream>
#include <map>
#include <string>
using namespace std;

int main() {
    // Initialize inventory with existing items
    map<string, int> inventory;
    inventory["apples"] = 50;
    inventory["bananas"] = 30;
    inventory["oranges"] = 25;
    inventory["grapes"] = 40;
    
    int n;
    cin >> n;
    
    for (int i = 0; i < n; i++) {
        string itemName;
        cin >> itemName;
        
        if (inventory.count(itemName)) {
            cout << itemName << ": " << inventory[itemName] << " in stock" << endl;
        } else {
            cout << itemName << ": Item not found" << endl;
        }
    }
    
    cout << "Total items in inventory: " << inventory.size() << endl;
    
    return 0;
}

All lessons in Logic & Flow