fork download
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4. using namespace std;
  5. // OrinTill Qimid CS1A Chapter 4, P. 294, 10
  6. //
  7. // /**************************************************************
  8. // * Calculate monthly budget.
  9. // *
  10. // * ____________________________________________________________
  11. // * This program will calculate the montly budget.
  12. // * ____________________________________________________________
  13. // * INPUT
  14. // * Student spending.
  15. // * OUTPUT
  16. // * Student monthly budget.
  17. // **************************************************************/
  18.  
  19. // Define the MonthlyBudget structure
  20. struct MonthlyBudget {
  21. double housing = 500.0;
  22. double utilities = 150.0;
  23. double householdExpenses = 65.0;
  24. double transportation = 50.0;
  25. double food = 250.0;
  26. double medical = 30.0;
  27. double insurance = 100.0;
  28. double entertainment = 150.0;
  29. double clothing = 75.0;
  30. double miscellaneous = 50.0;
  31. };
  32.  
  33. // Function to get user spending for the month
  34. void getSpending(MonthlyBudget &spent) {
  35. cout << "Enter the amounts spent in each category for the month:\n";
  36. cout << "Housing: ";
  37. cin >> spent.housing;
  38. cout << "Utilities: ";
  39. cin >> spent.utilities;
  40. cout << "Household Expenses: ";
  41. cin >> spent.householdExpenses;
  42. cout << "Transportation: ";
  43. cin >> spent.transportation;
  44. cout << "Food: ";
  45. cin >> spent.food;
  46. cout << "Medical: ";
  47. cin >> spent.medical;
  48. cout << "Insurance: ";
  49. cin >> spent.insurance;
  50. cout << "Entertainment: ";
  51. cin >> spent.entertainment;
  52. cout << "Clothing: ";
  53. cin >> spent.clothing;
  54. cout << "Miscellaneous: ";
  55. cin >> spent.miscellaneous;
  56. }
  57.  
  58. // Function to display the budget report
  59. void displayReport(const MonthlyBudget &budget, const MonthlyBudget &spent) {
  60. double totalBudget = budget.housing + budget.utilities + budget.householdExpenses +
  61. budget.transportation + budget.food + budget.medical +
  62. budget.insurance + budget.entertainment + budget.clothing +
  63. budget.miscellaneous;
  64.  
  65. double totalSpent = spent.housing + spent.utilities + spent.householdExpenses +
  66. spent.transportation + spent.food + spent.medical +
  67. spent.insurance + spent.entertainment + spent.clothing +
  68. spent.miscellaneous;
  69.  
  70. double totalDifference = totalBudget - totalSpent;
  71.  
  72. cout << fixed << setprecision(2);
  73. cout << "\nMonthly Budget Report:\n";
  74. cout << "--------------------------------------------\n";
  75. cout << "Category Budgeted Spent Over/Under\n";
  76. cout << "--------------------------------------------\n";
  77.  
  78. auto printCategory = [](string category, double budgeted, double spent) {
  79. double difference = budgeted - spent;
  80. cout << left << setw(15) << category << " "
  81. << setw(10) << budgeted << " "
  82. << setw(8) << spent << " "
  83. << (difference >= 0 ? "Under " : "Over ") << abs(difference) << endl;
  84. };
  85.  
  86. printCategory("Housing", budget.housing, spent.housing);
  87. printCategory("Utilities", budget.utilities, spent.utilities);
  88. printCategory("Household", budget.householdExpenses, spent.householdExpenses);
  89. printCategory("Transportation", budget.transportation, spent.transportation);
  90. printCategory("Food", budget.food, spent.food);
  91. printCategory("Medical", budget.medical, spent.medical);
  92. printCategory("Insurance", budget.insurance, spent.insurance);
  93. printCategory("Entertainment", budget.entertainment, spent.entertainment);
  94. printCategory("Clothing", budget.clothing, spent.clothing);
  95. printCategory("Miscellaneous", budget.miscellaneous, spent.miscellaneous);
  96.  
  97. cout << "--------------------------------------------\n";
  98. cout << "Total " << totalBudget << " " << totalSpent << " "
  99. << (totalDifference >= 0 ? "Under " : "Over ") << abs(totalDifference) << endl;
  100. }
  101.  
  102. int main() {
  103. // Create budget and spending structures
  104. MonthlyBudget budget;
  105. MonthlyBudget spent;
  106.  
  107. // Get spending details from the user
  108. getSpending(spent);
  109.  
  110. // Display the budget report
  111. displayReport(budget, spent);
  112.  
  113. return 0;
  114. }
Success #stdin #stdout 0.01s 5280KB
stdin
Standard input is empty
stdout
Enter the amounts spent in each category for the month:
Housing: Utilities: Household Expenses: Transportation: Food: Medical: Insurance: Entertainment: Clothing: Miscellaneous: 
Monthly Budget Report:
--------------------------------------------
Category          Budgeted    Spent    Over/Under
--------------------------------------------
Housing         500.00     500.00   Under 0.00
Utilities       150.00     150.00   Under 0.00
Household       65.00      65.00    Under 0.00
Transportation  50.00      50.00    Under 0.00
Food            250.00     250.00   Under 0.00
Medical         30.00      30.00    Under 0.00
Insurance       100.00     100.00   Under 0.00
Entertainment   150.00     150.00   Under 0.00
Clothing        75.00      75.00    Under 0.00
Miscellaneous   50.00      50.00    Under 0.00
--------------------------------------------
Total            1420.00 1420.00 Under 0.00