Removing Stock
Part of the Logic & Flow section of Coddy's C++ journey — lesson 33 of 56.
Challenge
EasyComplete 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: 50bananas: 30oranges: 25
The following inputs will be provided:
- An integer
nrepresenting the number of stock removal operations to perform - Then
npairs of inputs:- A string representing the item name
- An integer representing the quantity to remove
Your program should:
- Start with the initial inventory listed above
- Read the number of stock removal operations
- For each operation, read an item name and quantity to remove
- Use the
.count()method to check if the item exists in the inventory map - If the item doesn't exist, print an error message
- If the item exists, check if removing the quantity would make the stock negative
- If the removal would result in negative stock, print an error message
- If the removal is valid, subtract the quantity from the current stock and print a confirmation message
- 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 inventoryFor insufficient stock:
Error: Cannot remove [quantity] [item_name]. Only [current_stock] in stockFinal 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
1Pointers and Memory
What is a Pointer?Address-Of OperatorDereference OperatorNull PointersPointers and ArraysDynamic Memory with 'new'Freeing Memory with 'delete'Recap - Pointer Practice2Vectors (Dynamic Arrays)
Introducing std::vectorCreating a VectorAdding ElementsAccessing ElementsVector SizeIterating with a For LoopRange-Based For LoopRemoving ElementsRecap - Vector Operations5Project: Inventory Tool
Project SetupAdding and Updating Items