Calculation code problem

I was trying to calculate the age of a person. I used the following code to get the weeks and days left:

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
unsigned int date;	//Today's Date - User Input

unsigned int cur_month;	//The Current month
unsigned int cur_date;	//User's Input -  Today's date
unsigned int cur_days;	//Days in Current month

unsigned int dates;	//User Date (for Week)
unsigned int date_age;	//User Age Days
unsigned int week_age;	//User Age  Week

int months [12] = {31 , 28  , 31  , 30  , 31  , 30  , 31  , 31  , 30  , 31  , 30  , 31};

// To determine the number of days in the month depending on user input.
switch (cur_month)
{
case 1 : cur_days = months[0];
	break;
case 2 : cur_days = months[1];
	break;
case 3 : cur_days = months[2];
	break;
case 4 : cur_days = months[3];
	break;
case 5 : cur_days = months[4];
	break;
case 6 : cur_days = months[5];
	break;
case 7 : cur_days = months[6];
	break;
case 8 : cur_days = months[7];
	break;
case 9 : cur_days = months[8];
	break;
case 10 : cur_days = months[9];
	break;
case 11 : cur_days = months[10];
	break;
case 12 : cur_days = months[11];
	break;
}       

//............ code irrelevant to the problem...........
//Dates Calculation
dates = ((cur_days - cur_date) + date);

//Current Age - Weeks:
week_age = dates/7;

//Current Date_Age:
date_age = dates % 7;


The problem is that when I run the program, the output for age in days is 4294967290 and for weeks is 4294967295

I haven't been able to find the error in the code. Help?
can you post the whole code that you ran? or is it too long? =P
Last edited on
It is 330 lines long, hence I didn't post it here. But I have checked and made sure that he problem is somewhere in this part only. After the code, it simply prints the calculation. Though you can see the much older version of the code here: http://www.cplusplus.com/forum/general/40375/

I have added a number of thing in it though. Mainly that the program will work in months apart from April.

I think that the main problem lies in calculating dates.
4294967290 is the unsigned representation of -6 and 4294967295 -> -1. So 'cur_days' is likely less than 'cur_date'
I encounter the problem even if I use the cur_date as 6. And you can see from the above code that cur days is minimum 28 and maximum 31
Then I'd say that 'cur_days' and 'date' are 0. What's 'cur_month'?
cur_month is an Input by the user. It asks for the month number (1 for January and so on.) I used it in switch to determine the number of days in each month, using the array. it is not included in the code, but it is asked for by the program using cin. I also have set a loop over it if the entered value is larger than 12 or less than 1.
And one more thing, the problem is persistent for almost every date possible, it gave the correct output for 1, 2 and 3 and seven. But it gives the error if the date is larger, like 28, 26, 13, etc.
Last edited on
You need a debugger and look what the values are.

Btw you don't need that 'switch()' statement. You can write something like that
1
2
3
4
5
if((cur_month > 0) && (cur_month < 13))
{
  cur_days = months[cur_month - 1];
  ... // Doing the rest of your calculation
}
Which debugger should I use? I am using VC++ for compiling and testing the program out.
And thanks for the tip, it should reduce the code a lot.
Last edited on
VC++ comes with a built in debugger. Look at the menu and you should be able to find it
Can you tell me how to use the debugger? I couldn't find the option for it apart from the one I was already using (pressing F5), and how do I look for the values using the debugger?
My version is 2008. There it is

F9 -> remove/add breaktpoint (if you press F5 it runs until there)
F10 -> step into function
F11 -> step over function

how do I look for the values using the debugger?
If the execution stops (breakpoint / step) go to the variable with the mouse pointer and you can see the content.

There're also serveral windows (if there're not already open you can open them with the Debug menu) called auto, local and so forth where you can see the contents of the variables as well at the moment when program stops
I checked it out. I got the root of the problem:
it is for some reason setting the cur_days to 0. Though it should use the value from the array.
Can you find the error here?
Can you find the error here?
No not from what you posted. I can only guess that you may have declared 'cur_days' more than once in different scopes or another relevant variable?
I used search to find cur_days in the article. I found it three times. Once when declaring the variable, the second time when defining its value using cur_days = months[cur_month - 1]; and the third time in the equation for dates

Does the position of the variable effect its value? I have used it in the beginning of the int main function (which is also the only function in my code)

EDIT: I don't know why, but it worked after I moved it to just above the equation of dates.
Last edited on
Topic archived. No new replies allowed.