//Maxwell Brewer CS1A Chapter 11, p. 647, #11
//
/*******************************************************************************
* MONTHLY BUDGET TRACKER
* _____________________________________________________________________________
* This program helps track monthly expenses by comparing the user’s actual
* expenses against a predefined budget. It calculates whether the user is over
* or under budget for various expense categories and prints the results.
*
* INPUT:
* - User’s monthly expenses for categories such as housing, utilities, etc.
*
* OUTPUT:
* - Over or under budget amounts for each category.
* - Total monthly over or under budget amount.
*******************************************************************************/
#include <iostream>
#include <cmath> // For abs()
using namespace std;
// Structure to hold monthly expenses
struct MonthlyExpenses {
double housing;
double utilities;
double household;
double transport;
double food;
double medical;
double insurance;
double entertainment;
double clothing;
double misc;
};
// Function prototypes
void getInputExpenses(MonthlyExpenses &expenses);
void compareAndDisplayExpenses(const MonthlyExpenses &budget, const MonthlyExpenses &actual);
double calculateDifference(double budgeted, double actual);
int main() {
// Predefined budget
MonthlyExpenses budget = {
500, 150, 65, 50, 250, 30, 100, 150, 75, 50
};
// User's actual monthly expenses
MonthlyExpenses actualExpenses;
// Input user expenses
cout << "Enter your expenses for the current month:\n";
getInputExpenses(actualExpenses);
// Compare and display the results
cout << "\n\nBudget Comparison:\n";
cout << "-----------------------------------------\n";
compareAndDisplayExpenses(budget, actualExpenses);
return 0;
}
/**
* Reads user input for monthly expenses.
*
* @param expenses Reference to a MonthlyExpenses structure to store user input.
*/
void getInputExpenses(MonthlyExpenses &expenses) {
cout << "Housing: ";
cin >> expenses.housing;
cout << "Utilities: ";
cin >> expenses.utilities;
cout << "Household: ";
cin >> expenses.household;
cout << "Transport: ";
cin >> expenses.transport;
cout << "Food: ";
cin >> expenses.food;
cout << "Medical: ";
cin >> expenses.medical;
cout << "Insurance: ";
cin >> expenses.insurance;
cout << "Entertainment: ";
cin >> expenses.entertainment;
cout << "Clothing: ";
cin >> expenses.clothing;
cout << "Miscellaneous: ";
cin >> expenses.misc;
}
/**
* Compares and displays budgeted versus actual expenses.
*
* @param budget The predefined budget.
* @param actual The user's actual expenses.
*/
void compareAndDisplayExpenses(const MonthlyExpenses &budget, const MonthlyExpenses &actual) {
double totalDifference = 0;
cout << "Housing: ";
totalDifference += calculateDifference(budget.housing, actual.housing);
cout << "Utilities: ";
totalDifference += calculateDifference(budget.utilities, actual.utilities);
cout << "Household: ";
totalDifference += calculateDifference(budget.household, actual.household);
cout << "Transport: ";
totalDifference += calculateDifference(budget.transport, actual.transport);
cout << "Food: ";
totalDifference += calculateDifference(budget.food, actual.food);
cout << "Medical: ";
totalDifference += calculateDifference(budget.medical, actual.medical);
cout << "Insurance: ";
totalDifference += calculateDifference(budget.insurance, actual.insurance);
cout << "Entertainment: ";
totalDifference += calculateDifference(budget.entertainment, actual.entertainment);
cout << "Clothing: ";
totalDifference += calculateDifference(budget.clothing, actual.clothing);
cout << "Miscellaneous: ";
totalDifference += calculateDifference(budget.misc, actual.misc);
// Display total over/under budget amount
cout << "\n-----------------------------------------\n";
cout << "Total Monthly Difference: ";
if (totalDifference > 0) {
cout << "+$" << totalDifference << " (Under Budget)\n";
} else if (totalDifference < 0) {
cout << "-$" << abs(totalDifference) << " (Over Budget)\n";
} else {
cout << "$0 (Exact Budget)\n";
}
}
/**
* Calculates and prints the difference between budgeted and actual expenses.
*
* @param budgeted The budgeted amount for a category.
* @param actual The actual amount spent for a category.
* @return The difference (budgeted - actual) as a double.
*/
double calculateDifference(double budgeted, double actual) {
double difference = budgeted - actual;
if (difference > 0) {
cout << "+$" << difference << " (Under Budget)\n";
} else if (difference < 0) {
cout << "-$" << abs(difference) << " (Over Budget)\n";
} else {
cout << "$0 (Exact Budget)\n";
}
return difference;
}