Simple increment problem

So I'm currently developing a game that requires its own internal calender, I've written a function that updates the day month and year appropriately, however with the exception of January none of the months have 1sts and immediately start at the second. This should be an easy increment problem, but I've tried so many configurations and nothing seems to work. Anyone know what's going on?

heres my output

1
1
2010
1
2
2010
1
3
2010
1
4
2010
1
5
2010
1
6
2010
1
7
2010
1
8
2010
1
9
2010
1
10
2010
1
11
2010
1
12
2010
1
13
2010
1
14
2010
1
15
2010
1
16
2010
1
17
2010
1
18
2010
1
19
2010
1
20
2010
1
21
2010
1
22
2010
1
23
2010
1
24
2010
1
25
2010
1
26
2010
1
27
2010
1
28
2010
1
29
2010
1
30
2010
1
31
2010
2
2
2010
2
3
2010
2
4
2010
2
5
2010
2
6
2010
2
7
2010
2
8
2010
2
9
2010
2
10
2010
2
11
2010
2
12
2010
2
13
2010
2
14
2010
2
15
2010
2
16
2010
2
17
2010
2
18
2010
2
19
2010
2
20
2010
2
21
2010
2
22
2010
2
23
2010
2
24
2010
2
25
2010
2
26
2010
2
27
2010
2
28
2010
3
2
2010
3
3
2010
3
4
2010
3
5
2010
3
6
2010
3
7
2010
3
8
2010
3
9
2010
3
10
2010
3
11
2010
3
12
2010
3
13
2010
3
14
2010
3
15
2010
3
16
2010
3
17
2010
3
18
2010
3
19
2010
3
20
2010
3
21
2010
3
22
2010
3
23
2010
3
24
2010
3
25
2010
3
26
2010
3
27
2010
3
28
2010
3
29
2010
3
30
2010
3
31
2010
4
2
2010
4
3
2010
4
4
2010
4
5
2010
4
6
2010
4
7
2010
4
8
2010
4
9
2010
4
10
2010
4
11
2010
4
12
2010
4
13
2010
4
14
2010
4
15
2010
4
16
2010
4
17
2010
4
18
2010
4
19
2010
4
20
2010
4
21
2010
4
22
2010
4
23
2010
4
24
2010
4
25
2010
4
26
2010
4
27
2010
4
28
2010
4
29
2010
4
30
2010
5
2
2010
5
3
2010
5
4
2010
5
5
2010
5
6
2010
5
7
2010
5
8
2010
5
9
2010
5
10
2010
5
11
2010
5
12
2010
5
13
2010
5
14
2010
5
15
2010
5
16
2010
5
17
2010
5
18
2010
5
19
2010
5
20
2010
5
21
2010
5
22
2010
5
23
2010
5
24
2010
5
25
2010
5
26
2010
5
27
2010
5
28
2010
5
29
2010
5
30
2010
5
31
2010
6
2
2010
6
3
2010
6
4
2010
6
5
2010
6
6
2010
6
7
2010
6
8
2010
6
9
2010
6
10
2010
6
11
2010
6
12
2010
6
13
2010
6
14
2010
6
15
2010
6
16
2010
6
17
2010
6
18
2010
6
19
2010
6
20
2010
6
21
2010
6
22
2010
6
23
2010
6
24
2010
6
25
2010
6
26
2010
6
27
2010
6
28
2010
6
29
2010
6
30
2010
7
2
2010
7
3
2010
7
4
2010
7
5
2010
7
6
2010
7
7
2010
7
8
2010
7
9
2010
7
10
2010
7
11
2010
7
12
2010
7
13
2010
7
14
2010
7
15
2010
7
16
2010
7
17
2010
7
18
2010
7
19
2010
7
20
2010
7
21
2010
7
22
2010
7
23
2010
7
24
2010
7
25
2010
7
26
2010
7
27
2010
7
28
2010
7
29
2010
7
30
2010
7
31
2010
8
2
2010
8
3
2010
8
4
2010
8
5
2010
8
6
2010
8
7
2010
8
8
2010
8
9
2010
8
10
2010
8
11
2010
8
12
2010
8
13
2010
8
14
2010
8
15
2010
8
16
2010
8
17
2010
8
18
2010
8
19
2010
8
20
2010
8
21
2010
8
22
2010
8
23
2010
8
24
2010
8
25
2010
8
26
2010
8
27
2010
8
28
2010
8
29
2010
8
30
2010
8
31
2010
9
2
2010
9
3
2010
9
4
2010
9
5
2010
9
6
2010
9
7
2010
9
8
2010
9
9
2010
9
10
2010
9
11
2010
9
12
2010
9
13
2010
9
14
2010
9
15
2010
9
16
2010
9
17
2010
9
18
2010
9
19
2010
9
20
2010
9
21
2010
9
22
2010
9
23
2010
9
24
2010
9
25
2010
9
26
2010
9
27
2010
9
28
2010
9
29
2010
9
30
2010
10
2
2010
10
3
2010
10
4
2010
10
5
2010
10
6
2010
10
7
2010
10
8
2010
10
9
2010
10
10
2010
10
11
2010
10
12
2010
10
13
2010
10
14
2010
10
15
2010
10
16
2010
10
17
2010
10
18
2010
10
19
2010
10
20
2010
10
21
2010
10
22
2010
10
23
2010
10
24
2010
10
25
2010
10
26
2010
10
27
2010
10
28
2010
10
29
2010
10
30
2010
10
31
2010
11
2
2010
11
3
2010
11
4
2010
11
5
2010
11
6
2010
11
7
2010
11
8
2010
11
9
2010
11
10
2010
11
11
2010
11
12
2010
11
13
2010
11
14
2010
11
15
2010
11
16
2010
11
17
2010
11
18
2010
11
19
2010
11
20
2010
11
21
2010
11
22
2010
11
23
2010
11
24
2010
11
25
2010
11
26
2010
11
27
2010
11
28
2010
11
29
2010
11
30
2010
12
2
2010
12
3
2010
12
4
2010
12
5
2010
12
6
2010
12
7
2010
12
8
2010
12
9
2010
12
10
2010
12
11
2010
12
12
2010
12
13
2010
12
14
2010
12
15
2010
12
16
2010
12
17
2010
12
18
2010
12
19
2010
12
20
2010
12
21
2010
12
22
2010
12
23
2010
12
24
2010
12
25
2010
12
26
2010
12
27
2010
12
28
2010
12
29
2010
12
30
2010
12
31
2010
1
1
2011
1
2
2011
1
3
2011
1
4
2011
1
5
2011
1
6
2011
1
7
2011
1
8
2011
1
9
2011
1
10
2011
1
11
2011
1
12
2011
1
13
2011
1
14
2011
1
15
2011
1
16
2011
1
17
2011
1
18
2011
1
19
2011
1
20
2011
1
21
2011
1
22
2011
1
23
2011
1
24
2011
1
25
2011
1
26
2011
1
27
2011

and here's my code:

int day = 1; //1-31; depeding on the month
int month = 1; // 1-12 
int year = 2010;


void setup() {
  
  Serial.begin(9600);
}

void loop() {
   
  Serial.println(month);  
  Serial.println(day);
  Serial.println(year);
  delay(100);
  updateDate();
}

void updateDate(){// everyday we update the date to keep track of the game's internal calendar, this code can be optimized into three month sections 28, 30, and 31 day months
  
  if (month == 1){
  day++;
  if (day > 31){
    day = 1;
    ++month;
  }
  }
  if (month == 2){
  day++;
  if (day > 28){
   day = 1;
   ++month;
  }
  }
  if (month == 3){
  day++;
  if (day > 31){
    day = 1;
    ++month;
  }
  }
  if (month == 4){
  day++;
  if (day > 30){
    day = 1;
    ++month;
  }
  }
  if (month == 5){
  day++;
  if (day > 31){
    day = 1;
    ++month;
  }
  }
  if (month == 6){
  day++;
  if (day > 30){
    day = 1;
    ++month;
  }
  }
  if (month == 7){
  day++;
  if (day > 31){
    day = 1;
    ++month;
  }
  }
  if (month == 8){
  day++;
  if (day > 31){
    day = 1;
    ++month;
  }
  }
  if (month == 9){
  day++;
  if (day > 30){
    day = 1;
    ++month;
  }
  }
  if (month == 10){
  day++;
  if (day > 31){
    day = 1;
    ++month;
  }
  }
  if (month == 11){
  day++;
  if (day > 30){
    day = 1;
    ++month;
  }
  }
  if (month == 12){
  day++;
  if (day > 31){
    day = 1;
    ++year;//gotta update the year once december is over
    month = 1;
      }
    }
  }

Aside from that those if's for each month could be done with a switch and/or modulo's..

The problem is this:

if(month == 1) { month += 1; }
if(month == 2) { day += 1; }

The problem is that you use if after if, not if -> else if
So when month rolls over to the next, it may immediatly do the code for the next month aswell.

  if (month == 1){
  day++;
  if (day > 31){
    day = 1;
    ++month;
  }
  }
  if (month == 2){
  day++;
  if (day > 28){
   day = 1;
   ++month;
  }

When month = 1 and day = 31, month increments to 2, and day is set to 1.

Then, the next if block is encountered. month is 2, so day gets incremented again.

You need an else in front of each of the if's after the first one.

Even better is a switch statement, based on month.

switch(month)
{
   case 1:
      // do something
      break;
   case 2:
      // do something
      break;
   // etc.
}

thank you both for the advice, I have now solved the problem in If form by removing all of the day++s and placing a single one above the intial If statement in my function, however, if I replace the if functions with a switch case(yes, my grammar was proper) based on month (with day++ above the switch case) my month eternally remains January and the days infinitely increase. I'm currently satisfied, if anyone else would like to know more I will gladly share.

In the updateDate function, if you use a switch statement, for each month, see if it is the last day of the month. If so, increment month and set day to 0.

Then, increment day after the switch statement. The advantage of using a switch statement is that you need 12 cases, but many of the cases are the same:

switch(month)
{
   case 1:
   case 3:
   case 5:
   case 7:
   case 8:
   case 10:
   case 12:
      // Handle 31 day months
      break;

    case 2:
       // Handle 28 (or 29) day months
       break;

    case 4:
    case 6:
    case 9:
    case 11:
       // Handle 30 day months
}

Another way to do this is to have an array with the rollover days

byte maxDays[]={0,31,27,31,...};
(the zero is because arrays start at index zero)
then you can have
if (days++ > (int)maxDays[month] {
days =1;
if (month++ > 12) {
month=1;
year++
}
}

Thank you both for the advice :slight_smile: I have now reduced my function down to 7 lines and an array of bytes, the code is now much more manageable and space-saving (you will be credited when my project is complete) ;D