I have a program for school that I need help condensing some code, if possible. I have structure that has 10 fields for a monthly budget. I will have the user input their current budget and then my program informs the user if they are over under their budget and by how much. The code works fine, but can I some how get the variables displayed in the structure and not in the function? Also instead of having 10 If Else statements and 10 input fields, can i condense with a loop? I will place validation while loops in later and also fix the formatting. For now I am concerned on how to get the variables directly in the structure and how to use some loops to lower the amount of lines of code. Thanks in advance for any help.
#include <iostream>
#include <string>
using namespace std;
struct Budget
{
double housing, //Housing payment variable
utl, //Utitlities payment variable
houseExp, //household expenses payment variable
trans, //transportation expense payment variable
food, //food expense payment variable
med, //medicine expense payment variable
insur, //insurance expense payment variable
enterain, //entertainment expense payment variable
clothing, //clothing expense payment variable
misc; //miscellaneous expense varialbe.
};
//function Prototypes
void getCurrent(Budget&);
void compareCurrent(Budget);
void actualBudget(Budget&);
int main ()
{
Budget part;
getCurrent(part);
compareCurrent(part);
return 0;
}
void getCurrent (Budget &p)
{
double total = 0;
//int days = 10;
//for (int count = 1; count <= days; count++)
cout << "Please enter the housing budget: \n";
cin >> p.housing;
cout << "Please enter the Utilities amount: \n";
cin >> p.utl;
cout << "Please enter the Household Expenses amount: \n";
cin >> p.houseExp;
cout << "Please enter the Transportation amount: \n";
cin >> p.trans;
cout << "Please enter the Food amount: \n";
cin >> p.food;
cout << "Please enter the Medical amount: \n";
cin >> p.med;
cout << "Please enter the Insurance amount: \n";
cin >> p.insur;
cout << "Please enter the Entertainment amount: \n";
cin >> p.enterain;
cout << "Please enter the clothing amount: \n";
cin >> p.clothing;
cout << "Please enter the Miscellaneous amount: \n";
cin >> p.misc;
}
void compareCurrent(Budget p)
{
struct Budget standard;
standard.housing = 500.00;
standard.utl = 150.00;
standard.houseExp = 65.00;
standard.trans = 50.00;
standard.food = 250.00;
standard.med = 30.00;
standard.insur = 100.00;
standard.enterain = 150.00;
standard.clothing = 75.00;
standard.misc = 50.00;
double actualTotal = 1420.00 ;
double total = 0;
total = p.housing + p.utl + p.houseExp + p.trans + p.food + p.med + p.insur +
p.enterain + p.clothing + p.misc;
if (p.housing > standard.housing)
cout << "You are over your housing budget by $" << p.housing - standard.housing << endl;
else
cout << "Your housing budget is under by $ " << standard.housing - p.housing << endl;
if (p.utl > standard.utl)
cout << "You are over your Utilities budget by $" << p.utl - standard.utl << endl;
else
cout << "Your Utilities budget is fine." << endl;
if (p.houseExp > standard.houseExp)
cout << "You are over your Household Expenses budget by $" << p.houseExp - standard.houseExp << endl;
else
cout << "Your Household Expenses budget is fine." << endl;
if (p.trans > standard.trans)
cout << "You are over your Transportation Expenses budget by $" << p.trans - standard.trans << endl;
else
cout << "Your Transportation budget is fine." << endl;
if (p.food > standard.food)
cout << "You are over your Food Expenses budget by $" << p.food - standard.food << endl;
else
cout << "Your food Expenses budget is fine." << endl;
if (p.med > standard.med)
cout << "You are over your Medical Expenses budget by $" << p.med - standard.med << endl;
else
cout << "Your Household Medical budget is fine." << endl;
if (p.insur > standard.insur)
cout << "You are over your Insurance Expenses budget by $" << p.insur - standard.insur << endl;
else
cout << "Your Insurance Expenses budget is fine." << endl;
if (p.enterain > standard.enterain)
cout << "You are over your Entertrainment Expenses budget by $" << p.enterain - standard.enterain << endl;
else
cout << "Your Household Entertainment budget is fine." << endl;
if (p.clothing > standard.clothing)
cout << "You are over your Clothing Expenses budget by $" << p.clothing - standard.clothing << endl;
else
cout << "Your Clothing Expense budget is fine." << endl;
if (p.misc > standard.misc)
cout << "You are over your Miscellaneous Expenses budget by $" << p.misc - standard.misc << endl;
else
cout << "Your Miscellaneous Expenses budget is fine." << endl;
if (total > actualTotal)
cout << "You are over your total budget by: $" << total - actualTotal << endl;
else
cout << "Your Total Expense budget is fine." << endl;