fork download
  1. // Drew Duffey CSC5 Chapter 5, P.297, #19
  2. //
  3. /*******************************************************************************
  4.  *
  5.  * COMPUTE BUDGET
  6.  * _____________________________________________________________________________
  7.  * This program will take a budget amount, followed by a list of expenses, and
  8.  * determine how over/under budget the expenses are.
  9.  * _____________________________________________________________________________
  10.  * INPUT
  11.  * Budget : The total budget given
  12.  * Expense : The cost of an expense
  13.  *
  14.  * OUTPUT
  15.  * Over/Under : How over/under budget the expenses are
  16.  *
  17. *******************************************************************************/
  18. #include <iostream>
  19. #include <iomanip>
  20. using namespace std;
  21. int main ()
  22. {
  23. float budget;//INPUT - The total budget given
  24. float expense;//INPUT - The cost of an expense
  25. bool input;//CALC - Flag to check for the sentinel value
  26. float remaining;//OUTPUT - How over/under budget the expenses are
  27. float total;//CALC - Accumulator, the total amount of the expenses
  28.  
  29. //Input total budget
  30. cout << "Enter your total monthly budget: ";
  31. cin >> budget;
  32. cout << endl << endl;
  33. input = true;
  34.  
  35. //Loop for expense input
  36. while (input)
  37. {
  38. cout << "Please enter the amount of an expense for this month, " <<
  39. "or input a negative value to stop:\n";
  40. cin >> expense;
  41.  
  42. //Check for sentinel values, then break loop once one is entered
  43. if (expense <= 0)
  44. {
  45. input = false;
  46. break;
  47. }
  48.  
  49. //Add expense to total
  50. total += expense;
  51. }
  52.  
  53. //After looping, subtract expenses from total budget
  54. remaining = budget - total;
  55.  
  56. //Display how much over/under the user is
  57. cout << "\nYou have " << fixed << setprecision(2) << remaining <<
  58. " left, and are ";
  59.  
  60. if (remaining <= 0)
  61. {
  62. cout << "over budget.";
  63. }
  64. else
  65. {
  66. cout << "under budget.";
  67. }
  68. return 0;
  69. }
Success #stdin #stdout 0.01s 5320KB
stdin
50
75
-2
stdout
Enter your total monthly budget: 

Please enter the amount of an expense for this month, or input a negative value to stop:
Please enter the amount of an expense for this month, or input a negative value to stop:

You have -25.00 left, and are over budget.