cppbeginner10 minutes
Simple Console Budget Tracker in C++
Build a beginner-friendly console application that helps users track their daily expenses and remaining budget.
Challenge prompt
Create a C++ program that allows the user to enter their total budget for a day and then input multiple expenses one by one. After each expense, display the remaining budget. The user should be able to stop entering expenses by typing a sentinel value (e.g., 0). When the user finishes, display the total expenses and how much budget remains (or if they have overspent).
Guidance
- • Use a loop to continuously accept expense inputs until the user enters 0.
- • Maintain variables to keep track of total expenses and remaining budget.
- • Use simple input/output statements to interact with the user.
Hints
- • Initialize total expenses to 0 before starting to take inputs.
- • Subtract each expense from the remaining budget after input.
- • Check if the input is the sentinel value (0) inside the loop to stop taking expenses.
Starter code
#include <iostream>
using namespace std;
int main() {
double budget, expense, totalExpenses = 0;
cout << "Enter your total budget for the day: ";
cin >> budget;
cout << "Enter your expenses one by one (enter 0 to finish):\n";
while (true) {
cin >> expense;
// Your code to process each expense goes here
}
// Output total expenses and remaining budget
return 0;
}Expected output
Enter your total budget for the day: 100 Enter your expenses one by one (enter 0 to finish): 20 Remaining budget: 80 15.50 Remaining budget: 64.5 0 Total expenses: 35.5 Remaining budget: 64.5
Core concepts
variablesloopsconditionalsbasic I/O
Challenge a Friend
Send this duel to someone else and see if they can solve it.