In: Computer Science
Designing and implementing a C++ structure data type - Monthly Budget
Remember to use correct code formatting and documentation.
A student has established the following monthly budget:
Budget Categories Budgeted amount
-----------------------------------------------------
Housing $ 580.00
Utilities $ 150.00
Household Expenses $ 65.00
Transportation $ 50.00
Food $ 250.00
Medical $ 30.00
Insurance $ 100.00
Entertainment $ 150.00
Clothing $ 75.00
Miscellaneous $ 50.00
----------------------------------------------------
Write a program that declares a MonthlyBudget structure designed with member variables to hold each of these expense categories. The program should define two MonthlyBudget structure variables budget and spent. The first MonthlyBudget structure variable budget will contain (ie., be assigned) the budget figures given above. Pass this first structure >budget variable to a function displayBudget that will display the budget categories along with the budgeted amounts.
The second MonthlyBudget structure variable spent will be passed to another function getExpenses that should create a screen form that displays each category name and its budgeted amount, then positions the cursor next to it for the user to enter the amounts actually spent in each budget category during the past month.
Finally, the program should then pass both structure variables budget and spent to a function compareExpenses that displays a report indicating the amount over or under budget the student spent in each category, as well as the amount over or under for the entire monthly budget.
You may design your own user-input interface but you must use a structure and must implement the three functions listed above. The format of the monthly budgeted report should appear as similar as possible to the sample output shown below.
HINT: Use the setw and right manipulators to right-justify dollar value outputs in function compareExpenses.
INPUT VALIDATION: Do not accept negative values for the users actual expenditure inputs in function getExpenses.
A sample test run using the Code::Block IDE-CompilerHere is your monthly budget for YEAR 2020: Housing $ 580 Utilities $ 150 Household $ 65 Transportation $ 50 Food $ 250 Medical $ 30 Insurance $ 100 Entertainment $ 150 Clothing $ 75 Miscellaneous $ 50 ================================================= Total Budgeted $ 1500 ================================================= Enter month of expenditure: March Enter actual monthly expenditures for each budget category Housing: $ 580 Utilities: $ 130 Household: $ 50 Transportation:$ 50 Food: $ 230 Medical: $ 30 Insurance: $ 100 Entertainment: $ 120 Clothing: $ -10 ERROR: You must enter a positive number. Clothing: $ 100 Miscellaneous: $ 30 Budgeted Spent Difference ================================================= Housing 580.00 580.00 0.00 Utilities 150.00 130.00 -20.00 Household 65.00 50.00 -15.00 Transportation 50.00 50.00 0.00 Food 250.00 230.00 -20.00 Medical 30.00 30.00 0.00 Insurance 100.00 100.00 0.00 Entertainment 150.00 120.00 -30.00 Clothing 75.00 100.00 25.00 Miscellaneous 50.00 30.00 -20.00 ================================================= Total 1500.00 1420.00 80.00 ================================================= Congratulations! You were $80.00 under budget in March 2020. Process returned 0 (0x0) execution time : 104.850 s Press any key to continue.
Please make sure to declare your struct above your function prototypes and include appropriate function documentation as needed. You may use pointers or constant reference parameters when passing the structure variables to the functions mentioned above. DO NOT use global variables.
#include<iostream>
#include<iomanip>
using namespace std;
//structure defintion
struct MonthlyBudget
{
string categories[10];
float cost[10];
};
float diff[20],d_total=0,b_total=0,s_total=0;
string month;
//function prototypes
void displayBudget(MonthlyBudget);
void getExpenses(MonthlyBudget *);
void compareExpenses(MonthlyBudget,MonthlyBudget);
int main()
{
struct MonthlyBudget budget,spent;
//assigns the value of categories for budget structure
variable
budget={{"Housing","Utilities","Household
Expenses","Transportation","Food","Medical","Insurance","Entertainment","Clothing","Miscellaneous"
},{580,150,65,50,250,30,100,150,75,50}};
displayBudget(budget);
//assigns same values to spent structure variable's categories
member
for(int i=0;i<10;i++)
{
spent.categories[i]=budget.categories[i];
}
cout<<"Enter the month of expenditure : ";
cin>>month;
//function call by reference so that updations at the function will
reflect in the spent structure varible
getExpenses(&spent);
//function call by value
compareExpenses(budget,spent);
return 0;
}
void displayBudget(MonthlyBudget p)
{
cout<<left<<setw(50)<<"Budget
Categories"<<"Budgeted Amount";
cout<<"\n===================================================================\n";
for(int i=0;i<10;i++)
{
b_total=b_total+p.cost[i];
cout<<left<<setw(50)<<p.categories[i]<<"$"<<p.cost[i]<<"\n";
}
cout<<"\n===================================================================\n";
cout<<left<<setw(50)<<"Total
Budgeted"<<"$"<<b_total;
cout<<"\n===================================================================\n";
}
void getExpenses(MonthlyBudget *p)
{
cout<<"Enter actual monthly expenditures for each budget
category\n";
int i=0;
while(i<10)
{
cout<<left<<setw(50)<<p->categories[i];
cout<<"$";
cin>>p->cost[i];
if(p->cost[i]<0)
{
cout<<"ERROR:You must enter a positive number.\n";
}
else
{
i++;
}
}
}
void compareExpenses(MonthlyBudget budget,MonthlyBudget
spent)
{
cout<<left<<setw(25)<<"Categories"<<left<<setw(25)<<"Budgeted"<<left<<setw(25)<<"Spent"<<"Difference";
cout<<"\n=========================================================================================\n";
for(int i=0;i<10;i++)
{
diff[i]=(budget.cost[i]-spent.cost[i]);
d_total=d_total+diff[i];
s_total=s_total+spent.cost[i];
cout<<left<<setw(25)<<budget.categories[i]<<left<<setw(25)<<budget.cost[i]<<left<<setw(25)<<spent.cost[i]<<diff[i]<<"\n";
}
cout<<"============================================================================================\n";
cout<<left<<setw(25)<<"Total"<<left<<setw(25)<<b_total<<left<<setw(25)<<s_total<<d_total;
cout<<"\n============================================================================================\n";
if(d_total>0)
{
cout<<"Congratulations! You were "<<d_total<<"
under budget in "<<month<<" 2020.";
}
else if(d_total<0)
{
cout<<"Ooopss ! You were "<<-1*d_total<<" over
budget in "<<month<<" 2020." ;
}
else
{
cout<<"Not bad ! You have balanced budget in
"<<month<<" 2020." ;
}
}
OUTPUT
Budget Categories Budgeted Amount
===================================================================
Housing $580
Utilities $150
Household Expenses $65
Transportation $50
Food $250
Medical $30
Insurance $100
Entertainment $150
Clothing $75
Miscellaneous $50
===================================================================
Total Budgeted $1500
===================================================================
Enter the month of expenditure : March
Enter actual monthly expenditures for each budget category
Housing $580
Utilities $130
Household Expenses $50
Transportation $50
Food $230
Medical $30
Insurance $12 00
Entertainment $120
Clothing $-10
ERROR:You must enter a positive number.
Clothing $100
Miscellaneous $30
Categories Budgeted Spent Difference
=========================================================================================
Housing 580 580 0
Utilities 150 130 20
Household Expenses 65 50 15
Transportation 50 50 0
Food 250 230 20
Medical 30 30 0
Insurance 100 100 0
Entertainment 150 120 30
Clothing 75 100 -25
Miscellaneous 50 30 20
============================================================================================
Total 1500 1420 80
============================================================================================
Congratulations! You were 80 under budget in March 2020.