// Amari Mosley CSC5 Chapter 5, P. 297, #19
//
/**************************************************************
*
* ANALYZE BUDGET
*______________________________________________________________
* This program asks the user to enter their monthly budgeted amount.
* It then uses a loop to prompt the user to enter each expense
* for the month, keeps a running total, and displays
* whether the user is over or under their budget.
*
* INPUT
* monthlyBudget : Total amount budgeted for the month
* expense : Individual expense amounts entered in the loop
*
* OUTPUT
* Total Expense : Sum of all expenses entered
* Budget Status : Amount by which the user is over or under budget
*
**************************************************************/
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
// INPUT
double monthlyBudget;
double expense;
// OUTPUT / WORKING VARIABLES
double totalExpenses = 0.0;
// Prompt user for monthly budget
cout << "Enter the amount budgeted for the month: $";
cin >> monthlyBudget;
// Prompt user for expenses using a loop
// Enter a negative number or 0 to finish if desired,
// or use a sentinel/controlled loop structure.
// Here we prompt for expenses until the user indicates they are done
// by entering a negative value or 0, or we can use a sentinel value approach.
// Standard approach for this textbook problem: prompt for each expense,
// using a sentinel value (like 0 or a negative number) to stop.
cout << "\nEnter your monthly expenses (enter 0 or a negative number when finished):\n";
cout << "Enter an expense ($): ";
cin >> expense;
while (expense > 0) {
totalExpenses += expense;
cout << "Enter an expense ($): ";
cin >> expense;
}
// Display results with formatting
cout << fixed << setprecision(2);
cout << "\n-----------------------------------------\n";
cout << "Total Budgeted: $" << monthlyBudget << endl;
cout << "Total Expenses: $" << totalExpenses << endl;
cout << "-----------------------------------------\n";
if (totalExpenses < monthlyBudget) {
cout << "You are under budget by $" << (monthlyBudget - totalExpenses) << "." << endl;
} else if (totalExpenses > monthlyBudget) {
cout << "You are over budget by $" << (totalExpenses - monthlyBudget) << "." << endl;
} else {
cout << "You have spent exactly your budgeted amount." << endl;
}
return 0;
}