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

Budget Comparison:
-----------------------------------------
Housing: +$500 (Under Budget)
Utilities: +$150 (Under Budget)
Household: +$65 (Under Budget)
Transport: +$50 (Under Budget)
Food: +$250 (Under Budget)
Medical: +$30 (Under Budget)
Insurance: +$100 (Under Budget)
Entertainment: +$150 (Under Budget)
Clothing: +$75 (Under Budget)
Miscellaneous: +$50 (Under Budget)

-----------------------------------------
Total Monthly Difference: +$1420 (Under Budget)