Capstone C++ Workflows
Inventory Summary
A vector of small records can be summarized with a helper function and a range loop.
Inventory Summary
inventory_summary.cpp
#include <iostream>
#include <vector>
struct Item {
int count;
int price;
};
int totalValue(const std::vector<Item>& items) {
int total = 0;
for (const Item& item : items) {
total += item.count * item.price;
}
return total;
}
int main() {
int extraCount = ;
std::vector<Item> items{{3, 10}, {extraCount, 8}};
int value = totalValue(items);
bool stocked = value >= 40;
std::cout << "extraCount=" << extraCount << std::endl;
std::cout << "value=" << value << std::endl;
std::cout << "stocked=" << stocked << std::endl;
return 0;
}
#include <iostream>
#include <vector>
struct Item {
int count;
int price;
};
int totalValue(const std::vector<Item>& items) {
int total = 0;
for (const Item& item : items) {
total += item.count * item.price;
}
return total;
}
int main() {
int extraCount = ;
std::vector<Item> items{{3, 10}, {extraCount, 8}};
int value = totalValue(items);
bool stocked = value >= 40;
std::cout << "extraCount=" << extraCount << std::endl;
std::cout << "value=" << value << std::endl;
std::cout << "stocked=" << stocked << std::endl;
return 0;
}
#include <iostream>
#include <vector>
struct Item {
int count;
int price;
};
int totalValue(const std::vector<Item>& items) {
int total = 0;
for (const Item& item : items) {
total += item.count * item.price;
}
return total;
}
int main() {
int extraCount = ;
std::vector<Item> items{{3, 10}, {extraCount, 8}};
int value = totalValue(items);
bool stocked = value >= 40;
std::cout << "extraCount=" << extraCount << std::endl;
std::cout << "value=" << value << std::endl;
std::cout << "stocked=" << stocked << std::endl;
return 0;
}
records
A struct makes each inventory row clear: one count and one price.
summary
The helper loops over records and returns a single value for the workflow.