fork download
  1. // Torrez, Elaine CS1A Chapter 5 P. 297, #22
  2.  
  3. /******************************************************************************************
  4.  *
  5.  * BUDGET ANALYSIS
  6.  *
  7.  * --------------------------------------------------------------------------------
  8.  * This program asks the user to enter the amount budgeted for a month and then
  9.  * prompts the user to enter their expenses one by one. The program keeps a running
  10.  * total of expenses and, when finished, displays whether the user is over or under
  11.  * budget, and by how much.
  12.  * --------------------------------------------------------------------------------
  13.  *
  14.  * INPUT
  15.  * budgetedAmount : Amount the user planned to spend for the month
  16.  * expense : Each expense entered by the user
  17.  *
  18.  * OUTPUT
  19.  * totalExpenses : The total of all entered expenses
  20.  * difference : Amount over or under budget
  21.  *
  22.  *******************************************************************************************/
  23.  
  24. #include <iostream>
  25. #include <iomanip>
  26. #include <limits>
  27. using namespace std;
  28.  
  29. int main()
  30. {
  31. double budgetedAmount; // INPUT - User’s monthly budget
  32. double expense; // INPUT - Single expense value
  33. double totalExpenses = 0; // PROCESS - Running total of expenses
  34. char moreExpenses; // CONTROL - User’s choice to continue entering expenses
  35.  
  36. // --- Get the monthly budget ---
  37. cout << "Enter the amount budgeted for this month: $";
  38. while (!(cin >> budgetedAmount) || budgetedAmount < 0)
  39. {
  40. cout << "ERROR: Enter a positive number: $";
  41. cin.clear();
  42. cin.ignore(numeric_limits<streamsize>::max(), '\n');
  43. }
  44.  
  45. cout << "\nEnter your expenses one at a time.\n";
  46.  
  47. // --- Expense input loop ---
  48. do
  49. {
  50. cout << "Enter an expense amount: $";
  51. while (!(cin >> expense) || expense < 0)
  52. {
  53. cout << "ERROR: Enter a positive number: $";
  54. cin.clear();
  55. cin.ignore(numeric_limits<streamsize>::max(), '\n');
  56. }
  57.  
  58. totalExpenses += expense;
  59.  
  60. cout << "Do you have another expense? (Y/N): ";
  61. cin >> moreExpenses;
  62. }
  63. while (moreExpenses == 'Y' || moreExpenses == 'y');
  64.  
  65. // --- Calculate difference ---
  66. double difference = budgetedAmount - totalExpenses;
  67.  
  68. cout << fixed << setprecision(2);
  69. cout << "\n========== BUDGET SUMMARY ==========\n";
  70. cout << "Budgeted: $" << budgetedAmount << endl;
  71. cout << "Spent: $" << totalExpenses << endl;
  72.  
  73. if (difference > 0)
  74. cout << "You are under budget by $" << difference << "!" << endl;
  75. else if (difference < 0)
  76. cout << "You are over budget by $" << (-difference) << "!" << endl;
  77. else
  78. cout << "You are exactly on budget!" << endl;
  79.  
  80. cout << "====================================\n";
  81.  
  82. return 0;
  83. }
  84.  
Success #stdin #stdout 0.01s 5320KB
stdin
1000
200
300
150
n
stdout
Enter the amount budgeted for this month: $
Enter your expenses one at a time.
Enter an expense amount: $Do you have another expense? (Y/N): 
========== BUDGET SUMMARY ==========
Budgeted: $1000.00
Spent:    $200.00
You are under budget by $800.00!
====================================