fork download
  1. //Adhira B Cs1a CH5 hw
  2. //
  3. /*******************************************************************************
  4.  * COMPUTE BUDGET
  5.  * _____________________________________________________________________________
  6.  * program computes the amount of money owed to expenses and calculates
  7.  * the person's budget.
  8.  *
  9.  * Input
  10.  * budget : maximum budget in dollars
  11.  * expense : money owed to an expense
  12.  * expenses : Total amount of money owed for expenses
  13.  *
  14.  * Output
  15.  * minustotal : Total amount of money left in budget after expenses
  16.  *
  17.  ******************************************************************************/
  18. #include <iostream>
  19. #include <cmath>
  20. #include <iomanip>
  21. using namespace std;
  22.  
  23. int main() {
  24.  
  25. float budget;
  26. float expense; // one expense
  27. float expenses; // total expenses
  28. float total; //OUTPUT, remaining $ in budget
  29.  
  30. cout << "what is your budget for this month?\n";
  31. cin >> budget;
  32.  
  33. cout << "enter each of your expenses for the month\n";
  34. cout << "and then enter 0 when finished.\n\n";
  35. cin >> expense;
  36.  
  37. while (expense != 0)
  38. {
  39. // input validation
  40. if (expense < 0)
  41. {
  42. cout << "please enter a positive expense amount.\n";
  43. }
  44. else
  45. {
  46. expenses += expense;
  47. }
  48. cout << "enter the next expense.\n";
  49. cin >> expense;
  50. }
  51. total = budget - expenses;
  52. cout << fixed << showpoint << setprecision(2);
  53. if (total < 0)
  54. {
  55. cout << "\nthere is $" << abs(total) << " spent over the budget.\n";
  56. }
  57. else if (total > 0)
  58. {
  59. cout << "\nthere is $" << total << " left in the budget.\n";
  60. }
  61. else
  62. {
  63. cout << "no expenses!\n";
  64. }
  65. return 0;
  66. }
  67.  
Success #stdin #stdout 0.01s 5324KB
stdin
100
5 5 0
stdout
what is your budget for this month?
enter each of your expenses for the month
and then enter 0 when finished.

enter the next expense.
enter the next expense.

there is $90.00 left in the budget.