patterncppModerate
Managing student budgets
Viewed 0 times
budgetsstudentmanaging
Problem
I have just started to play around with C++ and I want to make my code efficient and clutter free. Please critique this code constructively and give feedback that will help me in my programming career.
```
#include
#include
#include
using namespace std;
/*
This structure will hold my
initialized structure set for
this assignment.
*/
struct Set_Budget
{
double set_housing;
double set_utilities;
double set_household_expenses;
double set_transportation;
double set_food;
double set_medical;
double set_insurance;
double set_entertainment;
double set_clothing;
double set_miscellaneous;
};
/*
This structure will receive
the values entered through
out this assignment.
*/
struct Asked_Budget
{
double asked_housing;
double asked_utilities;
double asked_household_expenses;
double asked_transportation;
double asked_food;
double asked_medical;
double asked_insurance;
double asked_entertainment;
double asked_clothing;
double asked_miscellaneous;
};
/*
My prototypes for the functions
which I will use in this program
*/
void amounts (Asked_Budget&); // Input passed by reference
void compare_budgets (Asked_Budget);
void budget_alert (double);
/*
This will me the main for this
program. It will only define
my " Asked_Budget " structure
and send its values to my
functions.
*/
void main ()
{
Asked_Budget student; // Define my Structure
amounts ( student ); // Sending Structure to function
compare_budgets ( student ); // Sending structure to function
}
/*
This Function will receive the values from
the user and access my Asked.Budget structure
to assign the received values
*/
void amounts ( Asked_Budget &amount) // Reference Parameter
{
cout > amount.asked_housing; // Assigning the value to the structure
cout > amount.asked_utilities;// Assigning the value to the structure
cout > amount.asked_household_expenses;// Assigning the value to the structure
cout > amount.asked_transportation;// Assigning the value to the structure
cout > amount.asked_food;// Assigning the va
```
#include
#include
#include
using namespace std;
/*
This structure will hold my
initialized structure set for
this assignment.
*/
struct Set_Budget
{
double set_housing;
double set_utilities;
double set_household_expenses;
double set_transportation;
double set_food;
double set_medical;
double set_insurance;
double set_entertainment;
double set_clothing;
double set_miscellaneous;
};
/*
This structure will receive
the values entered through
out this assignment.
*/
struct Asked_Budget
{
double asked_housing;
double asked_utilities;
double asked_household_expenses;
double asked_transportation;
double asked_food;
double asked_medical;
double asked_insurance;
double asked_entertainment;
double asked_clothing;
double asked_miscellaneous;
};
/*
My prototypes for the functions
which I will use in this program
*/
void amounts (Asked_Budget&); // Input passed by reference
void compare_budgets (Asked_Budget);
void budget_alert (double);
/*
This will me the main for this
program. It will only define
my " Asked_Budget " structure
and send its values to my
functions.
*/
void main ()
{
Asked_Budget student; // Define my Structure
amounts ( student ); // Sending Structure to function
compare_budgets ( student ); // Sending structure to function
}
/*
This Function will receive the values from
the user and access my Asked.Budget structure
to assign the received values
*/
void amounts ( Asked_Budget &amount) // Reference Parameter
{
cout > amount.asked_housing; // Assigning the value to the structure
cout > amount.asked_utilities;// Assigning the value to the structure
cout > amount.asked_household_expenses;// Assigning the value to the structure
cout > amount.asked_transportation;// Assigning the value to the structure
cout > amount.asked_food;// Assigning the va
Solution
The two structures are the same,
Don't make them different they are the same so just make one.
Then you can name each object differently;
Your input values assume the user is perfect and will make no mistakes.
I would change this to make sure that that you validate the user input (are negative numbers allowed). What happens if they type
Here you use a lot of variables that have the same names as structure.
I would just use another Budget object.
A lot of repeated code.
Encapsulate it into a function. That way fixes to the code only need to be applied to a single place (you don't need to do a find and replace to fix a bug).
struct Set_Budget
{};
struct Asked_Budget
{};Don't make them different they are the same so just make one.
struct Budget
{};Then you can name each object differently;
Budget set;
Budget asked;
Budget result;
... etcYour input values assume the user is perfect and will make no mistakes.
cout > amount.asked_housing;I would change this to make sure that that you validate the user input (are negative numbers allowed). What happens if they type
Blarp? I would then encapsulate that in a function.amount.housing = askUser(" PLEASE ENTER YOUR MONTHLY HOUSING EXPENSE: .......... $");Here you use a lot of variables that have the same names as structure.
// Variables to hold the results for each budget category
double result_housing;
double result_utilities;
double result_household_expenses;
double result_transportation;
double result_food;
double result_medical;
double result_insurance;
double result_entertainment;
double result_clothing;
double result_miscellaneous;I would just use another Budget object.
Budget result;A lot of repeated code.
cout << " RESULTING BUDGET DIFFERENCE FOR YOUR ( HOUSING ) CATEGORY: $" << fixed << setprecision (2) << result_housing; // I used set percision to reduce the output to 2 decimal places
budget_alert ( result_housing); // Sending the comparison variable to another function.
cout << " \n\n";Encapsulate it into a function. That way fixes to the code only need to be applied to a single place (you don't need to do a find and replace to fix a bug).
Code Snippets
struct Set_Budget
{};
struct Asked_Budget
{};struct Budget
{};Budget set;
Budget asked;
Budget result;
... etccout << " PLEASE ENTER YOUR MONTHLY HOUSING EXPENSE: .......... $";
cin >> amount.asked_housing;amount.housing = askUser(" PLEASE ENTER YOUR MONTHLY HOUSING EXPENSE: .......... $");Context
StackExchange Code Review Q#61801, answer score: 11
Revisions (0)
No revisions yet.