fork download
  1. //Diego Martinez CSC5 Chapter 5, P.297,#19
  2. /*******************************************************************************
  3. * DISPLAY BUDGET ANALYSIS
  4. * ______________________________________________________________________________
  5. * This program is designed to help a user compare their monthly spending to a
  6. * planned budget.
  7. *
  8. *
  9. * Computation is based on the Formula:
  10. * If the result is positive → you are under budget
  11. * If the result is negative → you are over budget
  12. * If the result is zero → you are exactly on budget
  13. *______________________________________________________________________________
  14. * INPUT
  15. * Budget : (one value)
  16. * Expenses : (multiple values)
  17. * Continue choice : (y or n)
  18. *
  19. * OUTPUT
  20. * Over budget message : user has spent more than the budget
  21. * Under budget message : user has spent less than the budget
  22. * On budget message : user spent exactly the budgeted amount
  23. *
  24. *******************************************************************************/
  25. #include <iostream>
  26. using namespace std;
  27.  
  28. int main() {
  29. float budget; // Stores the total monthly budget
  30. float expense; // Stores each individual expense
  31. float totalExpenses = 0; // Keeps a running total of all expenses
  32. char choice; // Stores the user’s response (y or n)
  33.  
  34. // Ask for monthly budget
  35. cout << "Enter your monthly budget: \n";
  36. cin >> budget;
  37.  
  38. // Loop to enter expenses
  39. do {
  40. cout << "Enter an expense: \n";
  41. cin >> expense;
  42.  
  43. totalExpenses += expense;
  44.  
  45. cout << "Do you have another expense to enter? (y/n): \n";
  46. cin >> choice;
  47.  
  48. } while (choice == 'y' || choice == 'Y');
  49.  
  50. // Calculate difference
  51. if (totalExpenses > budget) {
  52. cout << "You are over budget by $" << (totalExpenses - budget) << endl;
  53. } else if (totalExpenses < budget) {
  54. cout << "You are under budget by $" << (budget - totalExpenses) << endl;
  55. } else {
  56. cout << "You are exactly on budget." << endl;
  57. }
  58.  
  59. return 0;
  60. }
Success #stdin #stdout 0s 5320KB
stdin
300
40
y
100
y 
200
n


stdout
Enter your monthly budget: 
Enter an expense: 
Do you have another expense to enter? (y/n): 
Enter an expense: 
Do you have another expense to enter? (y/n): 
Enter an expense: 
Do you have another expense to enter? (y/n): 
You are over budget by $40