date system thingy...

Started by LordHart, Sun 27/06/2004 03:16:38

Previous topic - Next topic

LordHart

Okay, so I'm trying to set up a little test where when you press a button on a GUI it adds a day onto the date, if it is the last day of that month, then it adds a month and then sets the day back to 1, and then if it is the last month, it adds a year and sets the month back to 1.

Now, the problem is that it adds 1 to the days and months each time I press the button, but when it reaches the last month (12), the next time you press it, it adds the year and resets the month to 1.

So whats going on that is adding a month along with the days? I can't seem to figure it out.

Script Header:
Code: ags
int dayno = 1;
int monthno = 1;
int yearno = 2004;


Global Script:
Code: ags
function adddate() {
  if ((monthno == 4) || (monthno == 6) || (monthno == 9) || (monthno == 11)) {
    if (dayno < 30) {
      dayno += 1;
    } else {
      monthno += 1;
      dayno = 1;
    }
  } else if ((monthno == 1) || (monthno == 3) || (monthno == 5) || (monthno == 7) || (monthno == 8) || (monthno == 10) || (monthno == 12)) {
    if (dayno < 31) {
      dayno += 1;
    } else {
      monthno += 1;
      dayno = 1;
    }
  } else if ((monthno == 2)) {
    if (dayno < 28) {
      dayno += 1;
    } else {
      monthno += 1;
      dayno = 1;
    }
  }

  if ((monthno < 12)) {
    monthno += 1;
  } else {
    yearno += 1;
    monthno = 1;
  }
}

Wolfgang Abenteuer

#1
Code: ags

Ã,  else if ((monthno < 12)) {
Ã,  Ã,  monthno += 1;
Ã,  } else {
Ã,  Ã,  yearno += 1;
Ã,  Ã,  monthno = 1;
Ã,  }
}


This part looks like the problem.Ã,  Since it's a standard if statement and its only conditional is that monthno < 12, it'll get run every time the button is pressed and monthno is less than 12, which would be almost the whole time.Ã,  Each time it'll add one to monthno in addition to adding one to dayno earlier in the script.Ã,  You might just want to break out month 12 into a different else if { statement by itself, rather than having it in there twice (once earlier on, with all of the other 31-day months, and again at the end).Ã,  Just something like:


Code: ags
if (monthno == 12) {
Ã,  if (dayno < 31) {
Ã,  Ã,  dayno ++;
Ã,  }
Ã,  else {
Ã,  Ã,  dayno = 1;
Ã,  Ã,  monthno = 1;
Ã,  Ã,  yearno ++;
Ã,  }
}


That way, only the one code will get run, instead of two.Ã,  Would that work?

~Wolfgang

LordHart

#2
Yep, thats got it working properly. Thanks mate! :D

Hmm, now I just need to write out a way to have all the random stuff that will be happening happen... :P

edit: Actually, now I have to fix up the leap year thing. So in February, on leap years it will appear with 29 days... not 28.

SMF spam blocked by CleanTalk