gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

C/C++ > trouble with multiple if's

#97542 - spinal_cord - Thu Aug 10, 2006 11:10 am

Im tying to make an alarm clock, using the following code to check if its time to sound the alarm. However the alarm goes off every minute, regardless of the alarm time. Am I using the IF's correctly?

Code:

      if(alarm_hour==PA_RTC.Hour)
      {
         if(alarm_min==PA_RTC.Minutes)
         {
            if(alarm_set==1)
            {
               if(PA_RTC.Seconds==0)
                  {
                     //Do some sort of allert here!
                  }
            }
         }
      }                              


even if alarm_hour and PA_RTC.Hour are completely different, the alarm still happens, same with the minutes, although it does check if the seconds are zero.

What am i doing wrong?
_________________
I'm not a boring person, it's just that boring things keep happening to me.
Homepage

#97556 - Sausage Boy - Thu Aug 10, 2006 12:43 pm

An easier way would be to do

Code:

if ( alarm_hour==PA_RTC.hour && alarm_min==PA_RTC.Minutes && ...andsoonandsoon...)
{
//Do stuff
}


However, that won't solve your problem. Perhaps you need to update something to get the correct values, or maybe there's a bug in PALib.
_________________
"no offense, but this is the gayest game ever"

#97766 - sgeos - Fri Aug 11, 2006 1:43 pm

Looks OK to me. I'd find a way to test each condition individually.

You do realize that:
Code:
if (x==1)
and:
Code:
if (x)
Usually mean the same thing in C. (The first stricly tests for one, the second for any non-zero value).

This:
Code:
if (y==0)
and:
Code:
if (!y)
Always mean the same thing in C.

I'd probably make isAlarm(), isAlarmHour(), isAlarmMinute(), isAlarmSecond(), isAlarmSet() functions and test them individually. You can use a debugger, print, watch memory or whatever else you need to do to figure out what is going on.

Code:
void yourNewFunction(void)
{
  if ( isAlarm() )
  {
    // Do some sort of allert here!
  }
}

int isAlarm()
{
  return isAlarmHour() && isAlarmMinute() && isAlarmSecond() && isAlarmSet();
}

int isAlarmHour(void)
{
  return alarm_hour == PA_RTC.Hour;
}

int isAlarmMinute(void)
{
  return alarm_min == PA_RTC.Minutes;
}

int isAlarmSecond(void)
{
  return !(PA_RTC.Seconds);
}

int isAlarmSet(void)
{
  return alarm_set;
}

-Brendan

#97800 - sajiimori - Fri Aug 11, 2006 4:58 pm

Um, I'd take that advice about "all nonzero values are equivalent" with the world's largest grain of salt. ;)

#97839 - spinal_cord - Fri Aug 11, 2006 9:02 pm

dont worry guys, a had a sneaky bit of code where i forgot about it, I started a snooze function, and well, forgot to finish it, i had set the snooze timer for 1 minute (while testing) so it restarted the alarm every minute.

How the hell did i miss that?

anyway, thanks for the help guys.
_________________
I'm not a boring person, it's just that boring things keep happening to me.
Homepage

#98396 - sgeos - Tue Aug 15, 2006 12:46 am

sajiimori wrote:
Um, I'd take that advice about "all nonzero values are equivalent" with the world's largest grain of salt. ;)

Does that still hold if one is using an integer to represent a boolean? (Which I really should have stated.)

-Brendan

#98409 - sajiimori - Tue Aug 15, 2006 2:33 am

I'm not going to split hairs. One should obviously pay attention to the difference between testing for nonzero and testing for a particular value.

#98568 - sgeos - Wed Aug 16, 2006 2:17 am

I guess my main problem with the above implementation is 1 is being used as a magic number. I assumed that it was being used as a boolean value, hence my reply. It could just as easily be being used for ALARM_SET, ALARM_UNSET or ALARM_ERROR.

-Brendan

#98573 - sajiimori - Wed Aug 16, 2006 3:24 am

I do agree about that.