Free course · Lesson 5 of 5
Problem Solving with C++
In this lesson you will
- Plan a small program as data and functions
- Build and test it step by step
Combine everything so far into one working program. In C++, planning matters even more than in higher-level languages: decide your data types and structures first, since changing them later often means touching several parts of the program at once.
- Sketch the data first: what values do you need, and what type is each one?
- Write and test one function at a time — don't try to write the whole program before compiling.
- Read compiler errors from the first one down; later errors are often side effects.
- Test with normal data, then edge cases (empty vector, zero, negative numbers).
💡 Practical Skill: This careful, incremental approach is exactly how professional C++ is written — small, compiled, tested increments, not one giant untested block of code.
#include <iostream>#include <vector>#include <string>#include <map> std::vector<std::string> checkStock(std::map<std::string, int>& inventory, int threshold) { std::vector<std::string> lowStock; for (auto& pair : inventory) { if (pair.second < threshold) { lowStock.push_back(pair.first); } } return lowStock;} int main() { std::map<std::string, int> inventory = { {"exercise books", 120}, {"pens", 45}, {"chalk boxes", 8} }; std::vector<std::string> reorder = checkStock(inventory, 10); if (reorder.empty()) { std::cout << "All stock levels are healthy." << std::endl; } else { std::cout << "Reorder needed for: "; for (auto& item : reorder) std::cout << item << " "; std::cout << std::endl; } return 0;}
Practice exercise
Plan and write a program that records the daily water collected from a borehole for a week and prints the total and the lowest day.
Key takeaways
- Plan the data and functions before coding
- Compile and test after every small step
- Handle invalid input so the program never crashes
Free self-study lesson — no certificate. For the full instructor-led program, see registration options.