neewb's Profile
Reputation: 0
Apprentice
- Group:
- Members
- Active Posts:
- 46 (0.21 per day)
- Joined:
- 16-October 12
- Profile Views:
- 210
- Last Active:
Mar 07 2013 01:53 PM- Currently:
- Offline
Previous Fields
- Dream Kudos:
- 0
Posts I've Made
-
In Topic: Monthly Budget Program
Posted 6 Mar 2013
David W, on 06 March 2013 - 07:48 PM, said:Just to 'round out' my suggestion about the simpler code gained ...
when 'traversing data elements' in a loop ...
(when all the data elements are of the same type, as in this example),
and to show something of the power and facility that can be gained in C++ by using 'overloaded operators' ...
you may like to see (or keep for later) ... this next little example of a solution to a 'problem' very much like yours.
// traverseStructBudget.cpp // 2013-03-06 // #include <iostream> #include <iomanip> #include <string> using namespace std; struct Budget { public: // ctor ... Budget( double h=0, double c=0, double f=0, double p=0, double m=0 ) : house(h), car(c), food(f), phone(p), misc(m) {} // overloaded [] ... double operator [] (int i) const // read only ... { if( i == 0 ) return house; if( i == 1 ) return car; if( i == 2 ) return food; if( i == 3 ) return phone; if( i == 4 ) return misc; return 0.0; } private: double house, car, food, phone, misc; static const string items[]; static const int num_items; friend void report( const Budget& ref, const Budget& act); } ; // define here ... (outside) ... const string Budget::items[] = { "House", "Car", "Food", "Phone", "Misc" }; const int Budget::num_items = sizeof(Budget::items) / sizeof(Budget::items[0]); // definition of friend function ... void report( const Budget& ref, const Budget& act ) { double grand_surplus = 0.0; double grand_budget = 0.0; cout << setprecision(2) << fixed; // show 2 decimal places ... for( int i = 0; i < Budget::num_items; ++i ) { double surplus = ref[i] - act[i]; grand_surplus += surplus; grand_budget += ref[i]; cout << left << "For " << setw(10) << Budget::items[i] + ", " << right; if( surplus < 0 ) cout << " over budget of " << setw(8) << ref[i] << " by " << setw(8) << -surplus << endl; else cout << "under budget of " << setw(8) << ref[i] << " by " << setw(8) << surplus << endl; } cout << string( 55, '=' ) << endl; cout << string( 14, ' ' ); if( grand_surplus < 0 ) cout << " Over budget of " << setw(8) << grand_budget << " by " << setw(8) << -grand_surplus << endl; else cout << "Under budget of " << setw(8) << grand_budget << " by " << setw(8) << grand_surplus << endl; } int main() { Budget target(850, 250, 250, 125, 150); Budget actual(850, 350, 100, 140, 100); //for( int i = 0; i < Budget::num_items; ++i ) cout << target[i] << ' '; report( target, actual ); }
Good suggestion, and I will keep this in consideration once I get to the chapter with overloaded operators. I believe I am only a few chapters away. Thanks again. -
In Topic: Monthly Budget Program
Posted 5 Mar 2013
#define, on 04 March 2013 - 11:20 PM, said:Quote
You should check the instructions to ensure you comply with them. Here the amounts over or under are to be displayed.
Yes, you display whether the amounts are over or under, but not by how much.
Yes, you are correct, sorry I over sighted a little bit there, as you meant by "how much" not what the total figures were. I am going to go back to that and see if I can make that work, thanks again. -
In Topic: Monthly Budget Program
Posted 4 Mar 2013
David W, on 04 March 2013 - 01:02 PM, said:Quote
I cannot use arrays as this was a project before learning arrays. I have already taken this class, but I want to follow the directions as is.
You could use a few functions then ... with prototypes like ...
void takeIn( MonthlyBudget& nextMonth ); void show( const MonthlyBudget& month ); void compare( const MonthlyBudget& cmp, const MonthlyBudget& ref );
Yes I did something like that, giving both functions the structure variables as arguments per instructions. I only used two functions: one getter, and one display as that is what the instructions told me. Here is my completed code, it compiles and runs correctly. Like I told #define, would there be anything that you would have done differently, without using arrays of course?
// A student has established the following monthly budget: // Housing 500.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 with member variables to hold // each of these expense categories. The program should create two MonthlyBudget structure // variables. The first will hold the budget figures given above. The second will be passed // to a function that has the user enter the amounts actually spent in each budget category // during the past month. The program should then pass both structure variables to a function // 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. #include <iostream> using namespace std; struct MonthlyBudget { double housing, utilities, householdExpenses, transportation, food, medical, insurance, entertainment, clothing, miscellaneous; MonthlyBudget(double h= 0, double u= 0, double he = 0, double t= 0, double f= 0, double m= 0, double i= 0, double e= 0, double c= 0, double misc= 0) { housing = h; utilities = u; householdExpenses = he; transportation = t; food = f; medical = m; insurance = i; entertainment = e; clothing = c; miscellaneous = misc; } }; void getAmount(MonthlyBudget &); void displayAmount(MonthlyBudget &, MonthlyBudget &); int main() { MonthlyBudget utlty(500.00, 150.00, 65.00, 50.00, 250.00, 30.00, 100.00, 150.00, 75.00, 50.00); MonthlyBudget user; getAmount(user); displayAmount(user, utlty); system("pause"); return 0; } void getAmount(MonthlyBudget &user) { cout << endl << "Please enter your monthly expenses:\n"; cout << endl << "Housing: "; cin >> user.housing; cout << endl << "Utilities: "; cin >> user.utilities; cout << endl << "Household Expenses: "; cin >> user.householdExpenses; cout << endl << "Transportation: "; cin >> user.transportation; cout << endl << "Food: "; cin >> user.food; cout << endl << "Medical: "; cin >> user.medical; cout << endl << "Insurance: "; cin >> user.insurance; cout << endl << "Entertainment: "; cin >> user.entertainment; cout << endl << "Clothing: "; cin >> user.clothing; cout << endl << "Miscellaneous: "; cin >> user.miscellaneous; } void displayAmount(MonthlyBudget &user, MonthlyBudget &utlty) { cout << endl << "Here are your individual totals for the month:\n"; cout << endl << "Housing: " << "$ " << user.housing << endl; if (user.housing > utlty.housing) { cout << "Your housing expenses was over the budget\n"; } else { cout << "Your housing expenses was under the budget\n"; } cout << endl << "Utilities: " << "$ "<< user.utilities << endl; if (user.utilities > utlty.utilities) { cout << "Your utilities expenses was over the budget\n"; } else { cout << "Your utilities expenses was under the budget\n"; } cout << endl << "Household Expenses: " << "$ " << user.householdExpenses << endl; if (user.householdExpenses > utlty.householdExpenses) { cout << "Your household expenses was over the budget\n"; } else { cout << "Your household expenses was under the budget\n"; } cout << endl << "Transportation: " << "$ " << user.transportation << endl; if (user.transportation > utlty.transportation) { cout << "Your transportation expenses was over the budget\n"; } else { cout << "Your transportation expenses was under the budget\n"; } cout << endl << "Food: " << "$ " << user.food << endl; if (user.food > utlty.food) { cout << "Your food expenses was over the budget\n"; } else { cout << "Your food expenses was under the budget\n"; } cout << endl << "Medical:: " << "$ " << user.medical << endl; if(user.medical > utlty.medical) { cout << "Your medical expenses was over the budget\n"; } else { cout << "Your medical expenses was under the budget\n"; } cout << endl << "Insurance: " << "$ " << user.insurance << endl; if (user.insurance > utlty.insurance) { cout << "Your insurance expenses was over the budget\n"; } else { cout << "Your insurance expenses was under the budget\n"; } cout << endl << "Entertainment: " << "$ " << user.entertainment << endl; if (user.entertainment > utlty.entertainment) { cout << "Your entertainment expenses was over the budget\n"; } else { cout << "Your entertainment expenses was under the budget\n"; } cout << endl << "Clothing: " << "$ " << user.clothing << endl; if (user.clothing > utlty.clothing) { cout << "Your clothing expenses was over the budget\n"; } else { cout << "Your clothing expenses was under the budget\n"; } cout << endl << "Miscellaneous: " << "$ " << user.miscellaneous << endl; if (user.miscellaneous > utlty.miscellaneous) { cout << "Your miscellaneous expensees was over the budget\n"; } else { cout << "Your miscellaneous expenses was under the budget\n"; } double userBudget = user.housing + user.utilities + user.householdExpenses + user.transportation + user.food + user.medical + user.insurance + user.entertainment + user.clothing + user.miscellaneous; double utltyBudget = utlty.housing + utlty.utilities + utlty.householdExpenses + utlty.transportation + utlty.food + utlty.medical + utlty.insurance + utlty.entertainment + utlty.clothing + utlty.miscellaneous; cout << endl << "Total expenses for the month: $ " << userBudget; if(userBudget > utltyBudget) { cout << endl << "Your expenses exceed your monthly average budget\n"; } else { cout << endl << "Your expenses are under your monthly average budget\n"; } } -
In Topic: Monthly Budget Program
Posted 4 Mar 2013
#define, on 03 March 2013 - 06:42 PM, said:Although this could work :
54 MonthlyBudget getAmount();
the question asks for a structure to be passed to the get from user function.
So could be :
MonthlyBudget getAmount(MonthlyBudget user); // or void getAmount(MonthlyBudget &user);
To compare the structures fields you could do :
if (user.housing > expense.housing) { cout << "housing spend is over budget" << endl; }
Well, this is what I came up with. I used your idea with the two functions taking a reference of the structure variable, per instructions and then implementing the rest.It compiled and ran calculating correctly. Anything you would have done differently(without using arrays)?
// A student has established the following monthly budget: // Housing 500.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 with member variables to hold // each of these expense categories. The program should create two MonthlyBudget structure // variables. The first will hold the budget figures given above. The second will be passed // to a function that has the user enter the amounts actually spent in each budget category // during the past month. The program should then pass both structure variables to a function // 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. #include <iostream> using namespace std; struct MonthlyBudget { double housing, utilities, householdExpenses, transportation, food, medical, insurance, entertainment, clothing, miscellaneous; MonthlyBudget(double h= 0, double u= 0, double he = 0, double t= 0, double f= 0, double m= 0, double i= 0, double e= 0, double c= 0, double misc= 0) { housing = h; utilities = u; householdExpenses = he; transportation = t; food = f; medical = m; insurance = i; entertainment = e; clothing = c; miscellaneous = misc; } }; void getAmount(MonthlyBudget &); void displayAmount(MonthlyBudget &, MonthlyBudget &); int main() { MonthlyBudget utlty(500.00, 150.00, 65.00, 50.00, 250.00, 30.00, 100.00, 150.00, 75.00, 50.00); MonthlyBudget user; getAmount(user); displayAmount(user, utlty); system("pause"); return 0; } void getAmount(MonthlyBudget &user) { cout << endl << "Please enter your monthly expenses:\n"; cout << endl << "Housing: "; cin >> user.housing; cout << endl << "Utilities: "; cin >> user.utilities; cout << endl << "Household Expenses: "; cin >> user.householdExpenses; cout << endl << "Transportation: "; cin >> user.transportation; cout << endl << "Food: "; cin >> user.food; cout << endl << "Medical: "; cin >> user.medical; cout << endl << "Insurance: "; cin >> user.insurance; cout << endl << "Entertainment: "; cin >> user.entertainment; cout << endl << "Clothing: "; cin >> user.clothing; cout << endl << "Miscellaneous: "; cin >> user.miscellaneous; } void displayAmount(MonthlyBudget &user, MonthlyBudget &utlty) { cout << endl << "Here are your individual totals for the month:\n"; cout << endl << "Housing: " << "$ " << user.housing << endl; if (user.housing > utlty.housing) { cout << "Your housing expenses was over the budget\n"; } else { cout << "Your housing expenses was under the budget\n"; } cout << endl << "Utilities: " << "$ "<< user.utilities << endl; if (user.utilities > utlty.utilities) { cout << "Your utilities expenses was over the budget\n"; } else { cout << "Your utilities expenses was under the budget\n"; } cout << endl << "Household Expenses: " << "$ " << user.householdExpenses << endl; if (user.householdExpenses > utlty.householdExpenses) { cout << "Your household expenses was over the budget\n"; } else { cout << "Your household expenses was under the budget\n"; } cout << endl << "Transportation: " << "$ " << user.transportation << endl; if (user.transportation > utlty.transportation) { cout << "Your transportation expenses was over the budget\n"; } else { cout << "Your transportation expenses was under the budget\n"; } cout << endl << "Food: " << "$ " << user.food << endl; if (user.food > utlty.food) { cout << "Your food expenses was over the budget\n"; } else { cout << "Your food expenses was under the budget\n"; } cout << endl << "Medical:: " << "$ " << user.medical << endl; if(user.medical > utlty.medical) { cout << "Your medical expenses was over the budget\n"; } else { cout << "Your medical expenses was under the budget\n"; } cout << endl << "Insurance: " << "$ " << user.insurance << endl; if (user.insurance > utlty.insurance) { cout << "Your insurance expenses was over the budget\n"; } else { cout << "Your insurance expenses was under the budget\n"; } cout << endl << "Entertainment: " << "$ " << user.entertainment << endl; if (user.entertainment > utlty.entertainment) { cout << "Your entertainment expenses was over the budget\n"; } else { cout << "Your entertainment expenses was under the budget\n"; } cout << endl << "Clothing: " << "$ " << user.clothing << endl; if (user.clothing > utlty.clothing) { cout << "Your clothing expenses was over the budget\n"; } else { cout << "Your clothing expenses was under the budget\n"; } cout << endl << "Miscellaneous: " << "$ " << user.miscellaneous << endl; if (user.miscellaneous > utlty.miscellaneous) { cout << "Your miscellaneous expensees was over the budget\n"; } else { cout << "Your miscellaneous expenses was under the budget\n"; } double userBudget = user.housing + user.utilities + user.householdExpenses + user.transportation + user.food + user.medical + user.insurance + user.entertainment + user.clothing + user.miscellaneous; double utltyBudget = utlty.housing + utlty.utilities + utlty.householdExpenses + utlty.transportation + utlty.food + utlty.medical + utlty.insurance + utlty.entertainment + utlty.clothing + utlty.miscellaneous; cout << endl << "Total expenses for the month: $ " << userBudget; if(userBudget > utltyBudget) { cout << endl << "Your expenses exceed your monthly average budget\n"; } else { cout << endl << "Your expenses are under your monthly average budget\n"; } } -
In Topic: Monthly Budget Program
Posted 4 Mar 2013
neewb, on 03 March 2013 - 06:38 PM, said:This type of program lends itself to using arrays of values ... To facilitate looping though all the values and comparing them to a reference value.
const int numVals = 3; const double refVals[] = { 33.1, 22.1. 131.2 }; const double compVals[] = { 33, 22, 132 }; const string stuffs[] = { "Astuff", "Bstuff", "Cstuff" }; for( int i = 0: i < numVals; ++i ) { cout << "For " << stuffs[i] << ": "; if( compVals[i] > refVals[i] ) // then handle }
I cannot use arrays as this was a project before learning arrays. I have already taken this class, but I want to follow the directions as is.
My Information
- Member Title:
- New D.I.C Head
- Age:
- Age Unknown
- Birthday:
- Birthday Unknown
- Gender:
Contact Information
- E-mail:
- Click here to e-mail me
Friends
neewb hasn't added any friends yet.
|
|


Find Topics
Find Posts
View Reputation Given
|
Comments
neewb has no profile comments yet. Why not say hello?