Need help with this code (C++) A student has established the following monthly b
ID: 3745993 • Letter: N
Question
Need help with this code (C++) A student has established the following monthly budget Housing Utilities Transportation Food Entertainment Miscellaneous 500.00 150.00 50.00 250.00 150.00 50.00 Write a program that stores the following data about each budget category in a structure called Category: The name of the category The amount allocated to the category . The amount spent on that category so far this month The program should keep an array of these structures, one per category. When the program runs, it should initialize the array using the data above, and 0 for the amount spent so far this month Then it should repeat a loop that performs the following steps: The user should see a table of the categories with the allocated amounts, and amount spent so far this month. The user can choose to select a category, display the table, or quit. When a category is selected, the user can enter an amount of money to be added to the amount spent on that category so far this month. When the user quits the program, the program should display a message indicating the amount over or under for the entire monthly budget. Please see sample output in the file output1.pdf on the class website. Note: your output should look at least as nice as mine (data lined up in columns, formatted to two decimal points, et)Explanation / Answer
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. Thanks
//Code
#include<iostream>
using namespace std;
//structure to represent a category
struct Category{
string name;
double amountAllocated;
double amountSpent;
//constructor to initialize all fields
Category(string nm,double totalAmount,double amtSpent){
name=nm;
amountAllocated=totalAmount;
amountSpent=amtSpent;
}
};
//method to display all categories and their details
void displayTable(Category *categories, int size){
for(int i=0;i<size;i++){
cout<<"Category Name: "<<categories[i].name<<endl;
cout<<"Amount Allocated: $"<<categories[i].amountAllocated<<", Amount Spent: $"
<<categories[i].amountSpent;
//denoting user if any category's amount spent is over budget
if(categories[i].amountAllocated<categories[i].amountSpent){
cout<<" [--Over budget--]"<<endl;
}else{
cout<<endl;
}
}
}
//method to display the menu and return the choice
int showMenuGetChoice(){
int choice;
cout<<"1. Select Category"<<endl;
cout<<"2. Display Table"<<endl;
cout<<"3. Quit"<<endl;
cout<<"Enter your choice: ";
cin>>choice;
return choice;
}
//method to prompt the user to select a category, add money spent
void selectCategory( Category *categories, int size){
int categoryIndex;
cout<<"Choose a category (enter 1-6): ";
cin>>categoryIndex;
if(categoryIndex>0 && categoryIndex<=size){
categoryIndex--;
cout<<"Enter money to be added to amount spent: ";
double money;
cin>>money;
if(money>0){
categories[categoryIndex].amountSpent+=money;
cout<<"Added!"<<endl;
}else{
cout<<"Money cannot be negative!"<<endl;
}
}else{
cout<<"Invalid category!"<<endl;
}
}
//method to analyze the overall budget for the month, and display stats
void checkBudget(Category *categories, int size){
double totalAmtAllocated=0, totalSpent=0;
//finding total amount allocated and total amount spent
for(int i=0;i<size;i++){
totalAmtAllocated+=categories[i].amountAllocated;
totalSpent+=categories[i].amountSpent;
}
//displaying budget stats
if(totalSpent>totalAmtAllocated){
cout<<"Overall amount spent is over budget!"<<endl;
}else if(totalSpent<totalAmtAllocated){
cout<<"Overall amount spent is under budget!"<<endl;
}else{
cout<<"Overall amount spent is exact budget!"<<endl;
}
}
int main(){
//creating 6 categories
Category housing("Housing",500,0);
Category utilities("Utilities",150,0);
Category transportation("Transportation",50,0);
Category food("Food",250,0);
Category entertainment("Entertainment",150,0);
Category misc("Miscellaneous",50,0);
//adding all to an array
Category categories[] {housing,utilities,transportation,food,entertainment,misc};
int count=6; //current number of elements in array
int choice=0; //loop controller
//looping until user wishes to quit
while(choice!=3){
//getting choice
choice=showMenuGetChoice();
//performing actions based on choice
switch(choice){
case 1: selectCategory(categories,count);
break;
case 2: displayTable(categories,count);
break;
case 3: checkBudget(categories,count);
break;
default: cout<<"Invalid choice!"<<endl;
break;
}
}
return 0;
}
/*OUTPUT*/
1. Select Category
2. Display Table
3. Quit
Enter your choice: 2
Category Name: Housing
Amount Allocated: $500, Amount Spent: $0
Category Name: Utilities
Amount Allocated: $150, Amount Spent: $0
Category Name: Transportation
Amount Allocated: $50, Amount Spent: $0
Category Name: Food
Amount Allocated: $250, Amount Spent: $0
Category Name: Entertainment
Amount Allocated: $150, Amount Spent: $0
Category Name: Miscellaneous
Amount Allocated: $50, Amount Spent: $0
1. Select Category
2. Display Table
3. Quit
Enter your choice: 1
Choose a category (enter 1-6): 1
Enter money to be added to amount spent: 245.5
Added!
1. Select Category
2. Display Table
3. Quit
Enter your choice: 2
Category Name: Housing
Amount Allocated: $500, Amount Spent: $245.5
Category Name: Utilities
Amount Allocated: $150, Amount Spent: $0
Category Name: Transportation
Amount Allocated: $50, Amount Spent: $0
Category Name: Food
Amount Allocated: $250, Amount Spent: $0
Category Name: Entertainment
Amount Allocated: $150, Amount Spent: $0
Category Name: Miscellaneous
Amount Allocated: $50, Amount Spent: $0
1. Select Category
2. Display Table
3. Quit
Enter your choice: 1
Choose a category (enter 1-6): 4
Enter money to be added to amount spent: 270
Added!
1. Select Category
2. Display Table
3. Quit
Enter your choice: 2
Category Name: Housing
Amount Allocated: $500, Amount Spent: $245.5
Category Name: Utilities
Amount Allocated: $150, Amount Spent: $0
Category Name: Transportation
Amount Allocated: $50, Amount Spent: $0
Category Name: Food
Amount Allocated: $250, Amount Spent: $270 [--Over budget--]
Category Name: Entertainment
Amount Allocated: $150, Amount Spent: $0
Category Name: Miscellaneous
Amount Allocated: $50, Amount Spent: $0
1. Select Category
2. Display Table
3. Quit
Enter your choice: 3
Overall amount spent is under budget!