fork download
  1. //Cameron Pham CS1A Chapter 5, p. 297, #19
  2. //
  3. /******************************************************************************
  4. * ANALYZE BUDGET
  5. * ______________________________________________________________________________
  6. * This program accepts a floating point value for the amount budgeted for a
  7. * Month. A loop then accepts values for every expense for the month. It then
  8. * Evaluates whether the expenses are over or under the budget.
  9. * ______________________________________________________________________________
  10. * INPUT
  11. * Budget // Amount set for budget
  12. * Expenses // Every expense in the month
  13. *
  14. * OUTPUT
  15. * Difference // The amount that is under or over budget
  16. *
  17. *******************************************************************************/
  18. #include <iostream>
  19. #include <iomanip>
  20. using namespace std;
  21.  
  22. int main()
  23. {
  24. // Initialize variables
  25. double budget; // Amount budgeted for the month
  26. double expense; // Individual expense
  27. double totalExpenses = 0; // Running total of all expenses
  28. int numberOfExpenses; // Number of expenses to enter
  29.  
  30. // Ask the user to enter their monthly budget
  31. cout << "Enter the amount you have budgeted for the month: $";
  32. cin >> budget;
  33. cout << endl;
  34.  
  35. // Ask the user how many expenses they have
  36. cout << "Enter the number of expenses for the month: ";
  37. cin >> numberOfExpenses;
  38. cout << endl;
  39.  
  40. // Loop through each expense
  41. for (int count = 1; count <= numberOfExpenses; count++)
  42. {
  43. cout << "Enter expense " << count << ": $";
  44. cin >> expense;
  45. cout << endl;
  46.  
  47. // Add the current expense to the running total
  48. totalExpenses += expense;
  49. }
  50.  
  51. // Display the total expenses
  52. cout << fixed << setprecision(2);
  53. cout << endl;
  54. cout << "Total expenses: $" << totalExpenses << endl;
  55.  
  56. // Determine whether the user is over or under budget
  57. if (totalExpenses > budget)
  58. {
  59. cout << "You are over budget by $"
  60. << totalExpenses - budget << "." << endl;
  61. }
  62. else if (totalExpenses < budget)
  63. {
  64. cout << "You are under budget by $"
  65. << budget - totalExpenses << "." << endl;
  66. }
  67. else
  68. {
  69. cout << "You are exactly on budget." << endl;
  70. }
  71.  
  72. return 0;
  73. }
Success #stdin #stdout 0s 5320KB
stdin
800
5
200
400
130
123
85
stdout
Enter the amount you have budgeted for the month: $
Enter the number of expenses for the month: 
Enter expense 1: $
Enter expense 2: $
Enter expense 3: $
Enter expense 4: $
Enter expense 5: $

Total expenses: $938.00
You are over budget by $138.00.