fork download
  1. //Maxwell Brewer CS1A Chapter 11, p. 647, #11
  2. //
  3. /*******************************************************************************
  4.  * MONTHLY BUDGET TRACKER
  5.  * _____________________________________________________________________________
  6.  * This program helps track monthly expenses by comparing the user’s actual
  7.  * expenses against a predefined budget. It calculates whether the user is over
  8.  * or under budget for various expense categories and prints the results.
  9.  *
  10.  * INPUT:
  11.  * - User’s monthly expenses for categories such as housing, utilities, etc.
  12.  *
  13.  * OUTPUT:
  14.  * - Over or under budget amounts for each category.
  15.  * - Total monthly over or under budget amount.
  16.  *******************************************************************************/
  17.  
  18. #include <iostream>
  19. #include <cmath> // For abs()
  20. using namespace std;
  21.  
  22. // Structure to hold monthly expenses
  23. struct MonthlyExpenses {
  24. double housing;
  25. double utilities;
  26. double household;
  27. double transport;
  28. double food;
  29. double medical;
  30. double insurance;
  31. double entertainment;
  32. double clothing;
  33. double misc;
  34. };
  35.  
  36. // Function prototypes
  37. void getInputExpenses(MonthlyExpenses &expenses);
  38. void compareAndDisplayExpenses(const MonthlyExpenses &budget, const MonthlyExpenses &actual);
  39. double calculateDifference(double budgeted, double actual);
  40.  
  41. int main() {
  42. // Predefined budget
  43. MonthlyExpenses budget = {
  44. 500, 150, 65, 50, 250, 30, 100, 150, 75, 50
  45. };
  46.  
  47. // User's actual monthly expenses
  48. MonthlyExpenses actualExpenses;
  49.  
  50. // Input user expenses
  51. cout << "Enter your expenses for the current month:\n";
  52. getInputExpenses(actualExpenses);
  53.  
  54. // Compare and display the results
  55. cout << "\n\nBudget Comparison:\n";
  56. cout << "-----------------------------------------\n";
  57. compareAndDisplayExpenses(budget, actualExpenses);
  58.  
  59. return 0;
  60. }
  61.  
  62. /**
  63.  * Reads user input for monthly expenses.
  64.  *
  65.  * @param expenses Reference to a MonthlyExpenses structure to store user input.
  66.  */
  67. void getInputExpenses(MonthlyExpenses &expenses) {
  68. cout << "Housing: ";
  69. cin >> expenses.housing;
  70.  
  71. cout << "Utilities: ";
  72. cin >> expenses.utilities;
  73.  
  74. cout << "Household: ";
  75. cin >> expenses.household;
  76.  
  77. cout << "Transport: ";
  78. cin >> expenses.transport;
  79.  
  80. cout << "Food: ";
  81. cin >> expenses.food;
  82.  
  83. cout << "Medical: ";
  84. cin >> expenses.medical;
  85.  
  86. cout << "Insurance: ";
  87. cin >> expenses.insurance;
  88.  
  89. cout << "Entertainment: ";
  90. cin >> expenses.entertainment;
  91.  
  92. cout << "Clothing: ";
  93. cin >> expenses.clothing;
  94.  
  95. cout << "Miscellaneous: ";
  96. cin >> expenses.misc;
  97. }
  98.  
  99. /**
  100.  * Compares and displays budgeted versus actual expenses.
  101.  *
  102.  * @param budget The predefined budget.
  103.  * @param actual The user's actual expenses.
  104.  */
  105. void compareAndDisplayExpenses(const MonthlyExpenses &budget, const MonthlyExpenses &actual) {
  106. double totalDifference = 0;
  107.  
  108. cout << "Housing: ";
  109. totalDifference += calculateDifference(budget.housing, actual.housing);
  110.  
  111. cout << "Utilities: ";
  112. totalDifference += calculateDifference(budget.utilities, actual.utilities);
  113.  
  114. cout << "Household: ";
  115. totalDifference += calculateDifference(budget.household, actual.household);
  116.  
  117. cout << "Transport: ";
  118. totalDifference += calculateDifference(budget.transport, actual.transport);
  119.  
  120. cout << "Food: ";
  121. totalDifference += calculateDifference(budget.food, actual.food);
  122.  
  123. cout << "Medical: ";
  124. totalDifference += calculateDifference(budget.medical, actual.medical);
  125.  
  126. cout << "Insurance: ";
  127. totalDifference += calculateDifference(budget.insurance, actual.insurance);
  128.  
  129. cout << "Entertainment: ";
  130. totalDifference += calculateDifference(budget.entertainment, actual.entertainment);
  131.  
  132. cout << "Clothing: ";
  133. totalDifference += calculateDifference(budget.clothing, actual.clothing);
  134.  
  135. cout << "Miscellaneous: ";
  136. totalDifference += calculateDifference(budget.misc, actual.misc);
  137.  
  138. // Display total over/under budget amount
  139. cout << "\n-----------------------------------------\n";
  140. cout << "Total Monthly Difference: ";
  141. if (totalDifference > 0) {
  142. cout << "+$" << totalDifference << " (Under Budget)\n";
  143. } else if (totalDifference < 0) {
  144. cout << "-$" << abs(totalDifference) << " (Over Budget)\n";
  145. } else {
  146. cout << "$0 (Exact Budget)\n";
  147. }
  148. }
  149.  
  150. /**
  151.  * Calculates and prints the difference between budgeted and actual expenses.
  152.  *
  153.  * @param budgeted The budgeted amount for a category.
  154.  * @param actual The actual amount spent for a category.
  155.  * @return The difference (budgeted - actual) as a double.
  156.  */
  157. double calculateDifference(double budgeted, double actual) {
  158. double difference = budgeted - actual;
  159.  
  160. if (difference > 0) {
  161. cout << "+$" << difference << " (Under Budget)\n";
  162. } else if (difference < 0) {
  163. cout << "-$" << abs(difference) << " (Over Budget)\n";
  164. } else {
  165. cout << "$0 (Exact Budget)\n";
  166. }
  167.  
  168. return difference;
  169. }
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
Standard output is empty