fork download
  1. //Jacklyn Isordia CSC5 Chapter 5, P. 297, #19
  2. //
  3. /**************************************************************
  4.  *
  5.  * CALCULATE BUDGET ANALYSIS
  6.  * ____________________________________________________________
  7.  * This program asks the user to enter their monthly budget
  8.  * and then accepts multiple expenses until the user enters 0.
  9.  * Finally, it reports whether the user is over or under budget.
  10.  * ____________________________________________________________
  11.  * INPUT
  12.  * budget : The total monthly budget amount
  13.  * expense : Individual expenses to be added to the total
  14.  * * OUTPUT
  15.  * totalExpenses : The sum of all expenses entered
  16.  * status : A message indicating if the user is over,
  17.  * under, or exactly on budget.
  18.  * * Formula
  19.  * totalExpenses = totalExpenses + expense
  20.  *
  21.  **************************************************************/
  22.  
  23. #include <iostream>
  24. #include <iomanip>
  25. using namespace std;
  26.  
  27. int main() {
  28. double budget, expense, totalExpenses = 0.0;
  29.  
  30. cout << "Enter your budget for the month: $";
  31. cin >> budget;
  32.  
  33.  
  34. cout << "Enter your expenses (enter 0 to finish):" << endl;
  35. cout << endl;
  36.  
  37.  
  38. cout << "Expense: $";
  39. cin >> expense;
  40.  
  41. while (expense != 0) {
  42. totalExpenses += expense;
  43.  
  44.  
  45. cout << "Expense: $";
  46. cin >> expense;
  47. cout << endl;
  48. }
  49.  
  50.  
  51. cout << fixed << setprecision(2);
  52. cout << "\nTotal Budget: $" << budget << endl;
  53. cout << "Total Expenses: $" << totalExpenses << endl;
  54.  
  55. if (totalExpenses > budget) {
  56. cout << "You are over budget by $" << (totalExpenses - budget) << "." << endl;
  57. } else if (totalExpenses < budget) {
  58. cout << "You are under budget by $" << (budget - totalExpenses) << "." << endl;
  59. } else {
  60. cout << "You are exactly on budget!" << endl;
  61. }
  62.  
  63. return 0;
  64. }
Success #stdin #stdout 0.01s 5276KB
stdin
1000
250
100
50
0
stdout
Enter your budget for the month: $Enter your expenses (enter 0 to finish):

Expense: $Expense: $
Expense: $
Expense: $

Total Budget: $1000.00
Total Expenses: $400.00
You are under budget by $600.00.