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;

I am concerned on how to get the variables directly in the structure

Can you give an example (or more details) about what this means?

Can you give an example (or more details) about what this means?

can I directly input constant variables into a structure? for example:

struct Budget 
{
  double housing = 500.00,       //Housing payment variable
     utl = 150.00,               //Utitlities payment variable
     houseExp = 50.00,          //household expenses payment variable
     trans = 300.00,             //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.
};

Just a way to get a standard variable into my function.

You can either make them static or have a constructor with an initialization list

struct Budget 
{
  static double housing;
};

double Budget::housing = 500.00;

-or-
with the constructor

struct Budget 
{
  double housing,       //Housing payment variable
     utl,               //Utitlities payment variable
     houseExp;         //household expenses payment variable

    Budget() : housing(500.00),utl(50.00),houseExp(150.00)  {}
};

As far as your other if/else situation goes, there are probably some interesting ways to do it like with 3 parallel arrays that hold the category name, the standard amount, and the actual amount. The standard - actual calculation could be placed into a fourth array compared to zero and the categories in excess and deficit printed off.

You can either make them static or have a constructor with an initialization list

struct Budget 
{
  static double housing;
};

double Budget::housing = 500.00;

-or-
with the constructor

struct Budget 
{
  double housing,       //Housing payment variable
     utl,               //Utitlities payment variable
     houseExp;         //household expenses payment variable

    Budget() : housing(500.00),utl(50.00),houseExp(150.00)  {}
};

As far as your other if/else situation goes, there are probably some interesting ways to do it like with 3 parallel arrays that hold the category name, the standard amount, and the actual amount. The standard - actual calculation could be placed into a fourth array compared to zero and the categories in excess and deficit printed off.

Thank you for the help. I will try the 3 parrell array. Thank you for the help with the structure. Saved me alot of time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.