### Calculation Problem

I'm experiencing a problem within my program. I am to make a program that will let the user input 30 days of allowance and all of their accumulated expenses for that month then the program will subtract the allowance from the expenses. I am to use two dimensional arrays, pointers, structures and user-defined functions.

Long story short, I haven't gotten around the expenses part yet but I'm already experiencing a problem. Whenever I try my code, the sum of the allowance I input exceeds the actual answer. For example, I will input "1" for every day. Then the sum should be 30, correct? But the sum seems to exceed that. It seems to be a logical error on my part but I don't see it.

This is my code:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142`` ``````#include #include #include #include using namespace std; struct variables{int expenses; int allowance[1][30]; int sum; int *p;}; int main() { variables x; cout << "Please enter your daily allowance for this month: " << endl; for(int week=0;week<1;week++) { cout << "Week " << week+1 << endl; for (int day=0;day<30;day++) { cout << "Day " << day+1 << ": "; cin >> x.allowance[week][day]; } } for(int week=0;week<1;week++) { cout << "Week " << week+1 << endl; for (int day=0;day<30;day++) { cout << " " << x.allowance[week][day]; x.sum+=x.allowance[week][day]; }cout << endl; }cout << "Total allowance is: " << x.sum; system("pause>0"); system("cls"); cout << "Please enter your accumulated expenses for the whole month: "; cin >> x.expenses; system("pause>0"); return 0; }``````

Can anyone explain to me what I did wrong?

wrong!
`variables x;`

Right!
`variables x = {0};`

OR

`memset(&x,0,sizeof(x));`

I meant you should initialize the x to all-elements-zero

Last edited on
Wow! I never it would have been as simple as that. Thank you!
This code only executes once, but the inner loop goes 30 times (not 7 for a week?). The variable is called week, but there are 2 (not 4 - 1 for each week?) similar for loops. Wait, I'm confused !!!!

`for(int week=0;week<1;week++)`

So potentially you could have 60 days worth of expenses for 1 month!! This is also reflected in you `int allowance[1][30]` Arrays start at 0 - so this has 2 * 30 elements.

Hope this sheds some light on your issues.
Topic archived. No new replies allowed.