Project#3(a)-Designing and implementing a C++ structure data type: Monthly Budge
ID: 3817159 • Letter: P
Question
Project#3(a)-Designing and implementing a C++ structure data type: Monthly Budget (20 points)
A student has established the following monthly budget:
Write a program that declares a MonthlyBudget structure designed with member variables to hold each of these expense categories. The program should define two MonthlyBudget structure variables budget and spent. The first MonthlyBudget structure variable budget will contain (ie., be assigned) the budget figures given above. Pass this first structure budget variable to a function displayBudget that will display the budget categories along with the budgeted amounts.
The second MonthlyBudget structure variable spent will be passed to another function getExpenses that has the user enter the amounts actually spent in each budget category during the past month. Finally, the program should then pass both structure variables budget and spent to a function compareExpenses 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.
You may design your own user-input interface but you must use a structure and must implement the three functions listed above. The format of the monthly budgeted report should appear as similar as possible to the sample output shown below.
HINT: Use the setw and right manipulators (Gaddis 7EO, Chapter 3) to right-justify dollar value outputs in function compareExpenses.
INPUT VALIDATION: Do not accept negative values for the users actual expenditure inputs in function getExpenses.
Please make sure to include appropriate function documentation as needed (see handout on function documentation) and separate your functions with at least 1/2 inch of whitespace. You may use parameterized constructors for the initial budget amounts and constant reference parameters when passing the structure variables to the functions mentioned above (refer to struct related source code samples: struct1b.cpp, struct3a.cpp and struct3b.cpp). DO NOT use global variables. Turn in your source code followed by your program output. A single test-run output is sufficient.
Budget Categories Budgeted amount Housing $ 580.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 *(DO NOT use arrays, pointers or an array of structs)Explanation / Answer
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <sstream>
#include <algorithm>
struct MonthlyBudget
{
double housing,
utilities,
household_expenses,
transportation,
food,
medical,
insurance,
entertainment,
clothing,
miscellaneous;
};
// labels for each part of the MonthlyBudget structure
const char* MonthlyBudgetLabels[] = {
"Housing",
"Utilities",
"Household Expenses",
"Transportation",
"Food",
"Medical",
"Insurance",
"Entertainment",
"Clothing",
"Miscellaneous"
};
MonthlyBudget GOAL =
{
580.00,
150.00,
65.00,
50.00,
250.00,
30.00,
100.00,
150.00,
75.00,
50.00
};
double sum(MonthlyBudget& b)
{
double s = 0;
for (size_t i = 0; i < 10; i++) {
s += *((double *)(&b) + i);
}
return s;
}
double roundToMoney(double a) {
return floor((a * 100) + 0.5) / 100;
}
bool eq(double a, double b) {
return (fabs(a - b) <= 0.009);
}
void printCompare(double e, double goal) {
if (eq(e, goal)) {
std::cout << "Exactly on budget. ";
} else if (e > goal) {
std::cout << "$" << (e - goal) << " over budget. ";
} else {
std::cout << "$" << (goal - e) << " under budget. ";
}
return;
}
void displayDiffs(MonthlyBudget b) {
std::cout << "Budget Analysis "
"---------------------------------------------------- ";
for (size_t i = 0; i < 10; i++) {
std::cout << std::setw(18) << MonthlyBudgetLabels[i] << ": ";
printCompare( *((double *)(&b) + i), *((double *)(&GOAL) + i) );
}
std::cout << "---------------------------------------------------- "
"Overall Expenses: ";
printCompare( sum(b), sum(GOAL) );
return;
}
bool isNotNumChar(char c) {
static char number_chars[] = {'0', '1', '2', '3', '4', '5',
'6', '7', '8', '9', '.'};
return (std::count(number_chars, number_chars+11, c) == 0);
}
double processMoney(std::string raw) {
std::string processed (raw.size(), '');
std::remove_copy_if(raw.c_str(),
raw.c_str() + raw.size(),
processed.begin(),
isNotNumChar);
std::istringstream iss (processed);
double money = 0.0;
iss >> money;
return roundToMoney(money);
}
MonthlyBudget makeMonthlyBudget() {
MonthlyBudget b;
std::cout << "Enter the amounts spent in each category. ";
for (size_t i = 0; i < 10; i++) {
std::string unprocessed;
std::cout << MonthlyBudgetLabels[i] << ": ";
std::getline(std::cin, unprocessed);
*((double *)(&b) + i) = processMoney(unprocessed);
}
std::cout << ' ';
return b;
}
int main() {
displayDiffs(makeMonthlyBudget());
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.