fork download
  1. // Amari Mosley CSC5 Chapter 5, P. 297, #19
  2. //
  3. /**************************************************************
  4. *
  5. * ANALYZE BUDGET
  6. *______________________________________________________________
  7. * This program asks the user to enter their monthly budgeted amount.
  8. * It then uses a loop to prompt the user to enter each expense
  9. * for the month, keeps a running total, and displays
  10. * whether the user is over or under their budget.
  11. *
  12. * INPUT
  13. * monthlyBudget : Total amount budgeted for the month
  14. * expense : Individual expense amounts entered in the loop
  15. *
  16. * OUTPUT
  17. * Total Expense : Sum of all expenses entered
  18. * Budget Status : Amount by which the user is over or under budget
  19. *
  20. **************************************************************/
  21.  
  22. #include <iostream>
  23. #include <iomanip>
  24.  
  25. using namespace std;
  26.  
  27. int main() {
  28. // INPUT
  29. double monthlyBudget;
  30. double expense;
  31.  
  32. // OUTPUT / WORKING VARIABLES
  33. double totalExpenses = 0.0;
  34.  
  35. // Prompt user for monthly budget
  36. cout << "Enter the amount budgeted for the month: $";
  37. cin >> monthlyBudget;
  38.  
  39. // Prompt user for expenses using a loop
  40. // Enter a negative number or 0 to finish if desired,
  41. // or use a sentinel/controlled loop structure.
  42. // Here we prompt for expenses until the user indicates they are done
  43. // by entering a negative value or 0, or we can use a sentinel value approach.
  44. // Standard approach for this textbook problem: prompt for each expense,
  45. // using a sentinel value (like 0 or a negative number) to stop.
  46. cout << "\nEnter your monthly expenses (enter 0 or a negative number when finished):\n";
  47.  
  48. cout << "Enter an expense ($): ";
  49. cin >> expense;
  50.  
  51. while (expense > 0) {
  52. totalExpenses += expense;
  53. cout << "Enter an expense ($): ";
  54. cin >> expense;
  55. }
  56.  
  57. // Display results with formatting
  58. cout << fixed << setprecision(2);
  59. cout << "\n-----------------------------------------\n";
  60. cout << "Total Budgeted: $" << monthlyBudget << endl;
  61. cout << "Total Expenses: $" << totalExpenses << endl;
  62. cout << "-----------------------------------------\n";
  63.  
  64. if (totalExpenses < monthlyBudget) {
  65. cout << "You are under budget by $" << (monthlyBudget - totalExpenses) << "." << endl;
  66. } else if (totalExpenses > monthlyBudget) {
  67. cout << "You are over budget by $" << (totalExpenses - monthlyBudget) << "." << endl;
  68. } else {
  69. cout << "You have spent exactly your budgeted amount." << endl;
  70. }
  71.  
  72. return 0;
  73. }
Success #stdin #stdout 0s 5316KB
stdin
2000.00
1100.00
450.00
350.50
150.00
-1
stdout
Enter the amount budgeted for the month: $
Enter your monthly expenses (enter 0 or a negative number when finished):
Enter an expense ($): Enter an expense ($): Enter an expense ($): Enter an expense ($): Enter an expense ($): 
-----------------------------------------
Total Budgeted:   $2000.00
Total Expenses:   $2050.50
-----------------------------------------
You are over budget by $50.50.