#include <iostream>
#include <iomanip>
#include <cmath>
usingnamespace std;
constint NUM_EXP = 10;
constint EXP_AMT = 1500;
struct MonthlyBudget {
int housing;
int utilities;
int householdExpenses;
int transporation;
int food;
int medical;
int insurance;
int entertainment;
int clothing;
int miscellaneous;
int gross;
int under;
int over;
};
int main()
{
MonthlyBudget actual;
cout << "Enter amount for housing ";
cin >> actual.housing;
cout << "Enter amount for utilities ";
cin >> actual.utilities;
cout << "Enter amount for household expenses ";
cin >> actual.householdExpenses;
cout << "Enter amount for transportation ";
cin >> actual.transporation;
cout << "Enter amount for food ";
cin >> actual.food;
cout << "Enter amount for medical ";
cin >> actual.medical;
cout << "Enter amount for insurance ";
cin >> actual.insurance;
cout << "Enter amount for entertainment ";
cin >> actual.entertainment;
cout << "Enter amount for clothing ";
cin >> actual.clothing;
cout << "Enter amount for miscellaneous ";
cin >> actual.miscellaneous;
//Calculate the student's expenditures for the month
actual.gross +=(actual.housing + actual.utilities + actual.householdExpenses + actual.transporation + actual.food + actual.medical + actual.insurance + actual.entertainment + actual.clothing + actual.miscellaneous);
//To see if you are under budget
if (actual.gross < EXP_AMT)
{
actual.under += EXP_AMT - actual.gross;
cout << "You are below your monthly budget so you saved $ " << actual.under << endl;
}
//To see if you are right at your budget
if (EXP_AMT == actual.gross)
{
cout << "You are right at your monthly budget";
}
//To see if you are over the budget
if (EXP_AMT < actual.gross)
{
actual.over += actual.gross - EXP_AMT;
cout << "You are over your monthly budget by: $ " << actual.over <<endl;
}
system ("pause");
return 0;
}
When you write something like x += y; you're adding y to the value already stored in x, and then assigning the result to x.
+= should never be used if you haven't previously assigned some actual value to the variable on the left hand side.
The ordinary assignment operator = just assigns the value on the right hand side to the variable on the left, so it can safely be used to assign an initial value to a variable.
Look at your statements on lines 51, 56 and 69. Since you never initialized actual.gross, actual.under and actual.over, you're adding the sums on the RHS to some undefined random "garbage" value that just accidentally happened to be stored at the memory locations of those variables on the LHS.
I thought I at least initialized actual.gross by adding all of the categories to it. If that is not case what how should I do it instead? Sorry to ask that question I just thought that is how it is done for actual.gross
Ok thank you sorry to ask such I silly/stupid question. Now I want to make it so you can more then just one month so the loop I should is a do loop correct?