C++ Budget Calculator Project
Split a monthly income across needs, wants, and savings — and see the gaps.
What you will build
Enter a monthly income and your actual spending, and the calculator compares you against a plan like 50/30/20 (needs/wants/savings). It flags overspends and projects how long a savings goal takes — arithmetic with a point of view.
What you will learn
- Percentage-of-total calculations
- Comparing planned vs actual per category
- Goal projection (months to target)
- Clear, colour-free tabular reporting
Feature checklist
- Income input and category spending entry
- 50/30/20 target comparison per group
- Over/under flag for every category
- Savings-goal projection in months
- What-if mode: change income or a category, see the new plan
- Monthly data saved to a file
Starter code
#include <iostream>
#include <map>
#include <string>
int main() {
double income;
std::cout << "Monthly income: $"; std::cin >> income;
// group -> {target %, actual spent}
struct Line { double targetPct, actual; };
std::map<std::string, Line> plan = {
{"Needs (rent, food, transport)", {50, 0}},
{"Wants (fun, eating out)", {30, 0}},
{"Savings & debt", {20, 0}},
};
for (auto& [name, line] : plan) {
std::cout << "Spent on " << name << ": $";
std::cin >> line.actual;
}
std::cout << "\n";
for (const auto& [name, line] : plan) {
double target = income * line.targetPct / 100.0;
double diff = target - line.actual;
std::cout << name << "\n target $" << target
<< " | actual $" << line.actual
<< " | " << (diff >= 0 ? "under by $" : "OVER by $")
<< (diff >= 0 ? diff : -diff) << "\n";
}
}Full walkthrough
Step 1: Frame the plan around the 50/30/20 rule
The method: of monthly income, ~50% should go to Needs, ~30% to Wants, ~20% to Savings. struct Line { std::string category; Group group; double planned; } where Group is an enum { Need, Want, Saving }. The whole program is arithmetic ON this framing — get the model right and the reports write themselves.
Step 2: Read income and category lines
Prompt for monthly income, then loop reading category name, group (N/W/S), and amount, into a std::vector<Line> (and a std::map<Group, double> of group totals built alongside). Nothing here is new — the craft is in what you compute NEXT.
Step 3: Compute group totals and percentages
targets: Need = 0.50 × income, Want = 0.30 × income, Saving = 0.20 × income. For each group print actual vs target with the difference and a flag: OVER by X or under by X. A difference without a direction flag is a number; with the flag it is advice.
double target(Group g) {
switch (g) { case Need: return income * 0.50;
case Want: return income * 0.30;
case Saving: return income * 0.20; }
}Step 4: Flag every category, not just the groups
Within Needs, show each category's share of income too — rent at 40% of income is the story even when Needs-as-a-whole looks fine because food is low. Two levels of the same calculation; reuse one reporting function with different inputs.
Step 5: Project the savings goal
Ask for a goal amount; months = ceil(goal / monthlySaving) using the PLANNED saving line (or the target 20% if no saving line exists — say which you used). ceil matters: 500000 / 60000 = 8.33 months means the goal lands IN month 9, and telling a user '8 months' is a small lie that compounds.
Step 6: Add what-if mode
What-if re-runs the whole report with a modified income or one changed category WITHOUT touching the saved plan: copy the data, apply the change, recompute, print. The copy is the technique — experimentation must never mutate the real plan, the same discipline as minimax's undo and the formatter's dry runs.
Step 7: Test with round numbers
Income 300000: targets are 150000 / 90000 / 60000. Lines: Rent 120000 (Need), Food 45000 (Need), Data & transport 30000 (Want), Savings 60000 (Saving). Expect: Needs 165000 = 55% — OVER target by 15000; Wants 10% — under; Savings exactly on target; a 500000 goal projects to 9 months. If your flags disagree with your own arithmetic, the arithmetic in the code is wrong — trust the hand calculation.
Common mistakes
- Using integer division for months-to-goal and reporting 8 months for an 8.33-month goal.
- What-if mode editing the live plan, so exploring a pay cut permanently rewrites the user's budget.
- Comparing groups with floating == against targets instead of computing signed differences with an over/under flag.
Compile and run
g++ -std=c++17 -o budget budget.cppSample run
$ ./budget
Monthly income:
> 300000
Add lines as: category, group (N/W/S), amount — empty line to finish.
> Rent, N, 120000
> Food, N, 45000
> Data & transport, W, 30000
> Emergency fund, S, 60000
>
——— YOUR 50/30/20 REPORT (income 300000) ———
Needs: 165000 (55.0%) vs target 150000 — OVER by 15000
Wants: 30000 (10.0%) vs target 90000 — under by 60000
Savings: 60000 (20.0%) vs target 60000 — on target
Savings goal amount:
> 500000
At 60000/month you reach 500000 in 9 months.
What-if income (0 to skip):
> 0Try it in your browser
No browser demo is included for this project. Build the C++ version below — that is the real program.