Need help with the if statement

Once I enter all the data the only if statement that is working is the below budget. Is there something I forget to add for the other two?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
  #include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

const int NUM_EXP = 10;
const int 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

how should I do it instead?

Use the assignment operator (=).

 
actual.gross =(actual.housing + actual.utilities + actual.householdExpenses + actual.transporation + actual.food + actual.medical + actual.insurance + actual.entertainment + actual.clothing + actual.miscellaneous);
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?
Last edited on
Topic archived. No new replies allowed.