I had a reply to my work which is below and this is what my instructor said, i\'
ID: 3812007 • Letter: I
Question
I had a reply to my work which is below and this is what my instructor said, i'm not sure what to do.
PLEASE READ:Your code is very good, but you forgot to write an if statement following the indefinite input loop to print an appropriate message. Your program does somewhat implement this spec, but output like the following from your program is IMHO a bit obscure:
Your remaining budget is $-535.00
All that’s needed here to make this more readable is an if statement (I called this a Selection Statement in the Grading Rules below). Compare the two different messages I used in my results (in the Expected Output section below).
Missing selection statements to compare sum of expenses with the budget!
Expected Output:
--------------------
1. First Test Case:
Enter the month (eg, January, 2017): January, 2017
What is your budget for January, 2017 (eg, 1500): 1616
Enter your expenses for the month with Q to stop (eg, 40.53):
Expense: 309
Expense: 652
Expense: 200
Expense: 500
Expense: 150
Expense: 56
Expense: 109
Expense: 175
Expense: q
Budget Report for: January, 2017
-----------------------------
Careful, you've exceeded your budget by 535.00 dollars.
2. Second Test Case (abbreviated output is shown below):
Budget Report for: March, 2017
-----------------------------
Good, you have budgeted 22.00 more dollars than you spent.
Below is the code I made:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
double budget = 0;
double expenses = 0;
double total = 0;
cout << "What is your max limit for your budget for this month?";
cin >> budget;
cout << "Enter expenses (eg, 3.25) or -1 to stop: $";
cin >> expenses;
while ( expenses > 0 )
{
total += expenses;
cout << " Enter expenses (eg, 3.25) or -1 to stop: $";
cin >> expenses;
}
cout << showpoint << fixed;
cout << setprecision(2);
cout << endl;
cout << "The total cost of the "
<< "expenses is: $" << total << endl;
cout << "Your remaining budget is $" << budget - total;
cout << endl;
return 0;
}
/*
Budget Analysis
Design a program that asks the user to enter the amount that he or she has budgeted for a month (Programming Challenge, question #19, page 297) along with an indefinite input
loop for the monthÕs expenses.
The expenses input loop should accumulate the total of the expenses. When the expenses input loop finishes, the program should display an appropriate message comparing the
monthÕs expenses to the budget.
Include screenshots showing your program tested with the following two test cases:
January 2017 Monthly Budget of
Expenses: ------------------------------
$1616.00
Car payment
Rent:
Insurance: Supermarkets, food: Restaurants: Internet service: Cell phone: Gasoline:
$309.00
$652.00
$200.00
$500.00
$150.00
$56.00
$109.00
$175.00
March 2017 Monthly Budget of $1616.00
Expenses: ------------------------------
Rent:
Insurance: Supermarkets, food: Restaurants: Internet service: Cell phone: Gasoline:
$590.00
$200.00
$450.00
$100.00
$47.00
$32.00
$175.00
*/
Explanation / Answer
#include <iostream>
#include <iomanip>
#include <stdio.h>
using namespace std;
//Main function
int main()
{
char month[20];
double budget = 0;
double expenses = 0;
double total = 0;
//Accept the month and year
cout<<" Enter the month (e.g, January, 2017): ";
gets(month);
//Accepts budget
cout << "What is your budget for "<<month<<" (e.g, 1500): ";
cin >> budget;
//Loops until q or Q is entered
do
{
//Accept expenses
cout << "Enter your expenses for the month with Q to stop (e.g, 40.53):";
cin >> expenses;
if(expenses == 'q' || expenses == 'Q')
break;
//Calculates total expenses
total += expenses;
}while(expenses > 0);//End of loop
cout << showpoint << fixed;
cout << setprecision(2);
cout << endl;
//Displays the budget report
cout<<" Budget Report for: "<<month;
//Checks if total expenses is more than budget
if(total > budget)
cout<<" Careful, you have exceeded your budget by "<<(total - budget)<<" dollars.";
//Otherwise
else
cout<<" Good, you have budgeted "<<(budget - total)<<" more dollars than you spent.";
cout << endl;
return 0;
}//End of main
Sample Run1:
Enter the month (e.g, January, 2017): March, 2017
What is your budget for March, 2017 (e.g, 1500): 1616
Enter your expenses for the month with Q to stop (e.g, 40.53):309
Enter expenses (e.g, 3.25) or -1 to stop: $652
Enter expenses (e.g, 3.25) or -1 to stop: $200
Enter expenses (e.g, 3.25) or -1 to stop: $500
Enter expenses (e.g, 3.25) or -1 to stop: $150
Enter expenses (e.g, 3.25) or -1 to stop: $56
Enter expenses (e.g, 3.25) or -1 to stop: $109
Enter expenses (e.g, 3.25) or -1 to stop: $175
Enter expenses (e.g, 3.25) or -1 to stop: $q
Budget Report for: March, 2017
Careful, you have exceeded your budget by 535.00 dollars.
Sample Run2:
Enter the month (e.g, January, 2017): March, 2017
What is your budget for March, 2017 (e.g, 1500): 1616
Enter your expenses for the month with Q to stop (e.g, 40.53): $590
Enter your expenses for the month with Q to stop (e.g, 40.53): $200
Enter your expenses for the month with Q to stop (e.g, 40.53): $450
Enter your expenses for the month with Q to stop (e.g, 40.53): $100
Enter your expenses for the month with Q to stop (e.g, 40.53): $47
Enter your expenses for the month with Q to stop (e.g, 40.53): $32
Enter your expenses for the month with Q to stop (e.g, 40.53): $175
Enter your expenses for the month with Q to stop (e.g, 40.53): $q
Budget Report for: March, 2017
Good, you have budgeted 22.00 more dollars than you spent.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.