Simple Budget Tracker
Create a basic C++ program that helps track expenses and calculate the remaining budget. Users will input their total budget and then enter expenses one by one. The program will output the remaining budget after each expense and a summary at the end.
Challenge prompt
Write a C++ program that allows the user to: 1. Enter their total budget. 2. Enter multiple expenses one by one. After each expense, the program should display the remaining budget. 3. When the user types 0 as the expense amount, stop taking inputs and display the total expenses and the final remaining budget. Make sure to handle cases where the expense might exceed the remaining budget by displaying a warning message and not subtracting that expense.
Guidance
- • Use a loop to repeatedly take expense inputs until the user enters 0.
- • Keep track of expenses using a variable and update the remaining budget after each valid expense.
- • Use conditionals to ensure expenses that exceed the remaining budget are rejected with a warning.
Hints
- • Remember to initialize your variables such as total budget and total expenses.
- • Use a while loop or a do-while loop to keep asking for expenses.
- • Check if the expense entered is greater than the remaining budget before subtracting.
Starter code
#include <iostream>
using namespace std;
int main() {
double totalBudget, expense, totalExpenses = 0;
cout << "Enter your total budget: ";
cin >> totalBudget;
// Your code to input expenses and calculate remaining budget goes here
return 0;
}Expected output
Enter your total budget: 500 Enter an expense (0 to finish): 100 Remaining budget: 400 Enter an expense (0 to finish): 200 Remaining budget: 200 Enter an expense (0 to finish): 250 Expense exceeds remaining budget. Try a smaller amount. Enter an expense (0 to finish): 150 Remaining budget: 50 Enter an expense (0 to finish): 0 Total expenses: 450 Final remaining budget: 50
Core concepts
Challenge a Friend
Send this duel to someone else and see if they can solve it.