Printing the Inventory Report
Part of the Logic & Flow section of Coddy's C++ journey — lesson 34 of 56.
Challenge
EasyComplete your inventory tracking system by implementing a comprehensive inventory report feature. This final lesson demonstrates how to iterate through an entire map and present all stored data in a clean, organized format.
Start with the following inventory:
map<string, int> inventory = {
{"apples", 50},
{"bananas", 30},
{"oranges", 25},
{"grapes", 40},
{"strawberries", 15}
};Your program should:
- Generate a complete inventory report that displays all items and their current quantities
- Calculate and display the total number of different items in the inventory
- Calculate and display the total quantity of all items combined
- Identify and display the item with the highest quantity
- Identify and display the item with the lowest quantity
Use the following exact output format:
Inventory report header:
===== INVENTORY REPORT =====All items (in alphabetical order):
apples: 50
bananas: 30
grapes: 40
oranges: 25
strawberries: 15Summary statistics:
===== SUMMARY =====
Total items: 5
Total quantity: 160
Highest stock: apples (50)
Lowest stock: strawberries (15)Use a range-based for loop to iterate through the entire inventory map. For each key-value pair, access the item name with pair.first and the quantity with pair.second. The map automatically maintains alphabetical order when iterating, so no additional sorting is needed.
To find the lowest quantity item, initialize your lowestQuantity variable to INT_MAX (the largest possible integer value, available after including <climits> or <iostream>). This ensures the very first item's quantity will always be less than the starting value, so the comparison works correctly regardless of the actual quantities in the inventory.
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;
int n;
cin >> n;
for (int i = 0; i < n; i++) {
string itemName;
int quantity;
cin >> itemName >> quantity;
if (inventory.count(itemName) == 0) {
cout << "Error: " << itemName << " not found in inventory" << endl;
} else if (inventory[itemName] < quantity) {
cout << "Error: Cannot remove " << quantity << " " << itemName << ". Only " << inventory[itemName] << " in stock" << endl;
} else {
inventory[itemName] -= quantity;
cout << "Removed " << quantity << " " << itemName << ". New total: " << inventory[itemName] << endl;
}
}
cout << "Final Inventory:" << endl;
for (auto& item : inventory) {
cout << item.first << ": " << item.second << 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