Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ SMAUG ➜ SMAUG coding ➜ suggest parentheses around assignment used as truth value

suggest parentheses around assignment used as truth value

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Robert Powell   Australia  (367 posts)  Bio
Date Wed 13 Jul 2005 10:58 AM (UTC)
Message
Ok, i have a funny think happening and im not entirly sure why, hopeing someone will be abloe to help me on this.

Im adding things into time_update so i can have certain things happen on different days, i added in the following code and nothing happened, the days came and went and no call to my function.

if(time_info.day == 24)
    {
      do_mystuff();      
    }

To get it to work i changed it to look as below, now when it is the 24th do_mystuff is called and all works as it should, which brings me to the warning im getting on compile.

if(time_info.day = 24)
    {
      do_mystuff();      
    }

update.c:3131:warning:suggest parentheses around assignment used as truth value. Why isnt the first example with the == working as it should, or better yet, why is the single = working when its not the right way to compare values, or am i missing something totaly here.

Thanks in advance.

Just a guy having a bit of fun. Nothing more, nothing less, I do not need I WIN to feel validated.
Top

Posted by Samson   USA  (683 posts)  Bio
Date Reply #1 on Wed 13 Jul 2005 11:34 AM (UTC)
Message
With your first example, are you sure the clock reaches 24 and doesn't cycle at 23 instead? The midnight hour is usually 0.

Your second example is warning you that you're assigning the value 24 to something the compiler thinks you're using for a comparison - which is what you've done. Now instead of checking to see if it's 24, it will always be 24 which is why it works.

Go back to the first example and see if checking for == 23 will do what you want.
Top

Posted by Robert Powell   Australia  (367 posts)  Bio
Date Reply #2 on Wed 13 Jul 2005 09:46 PM (UTC)

Amended on Wed 13 Jul 2005 09:47 PM (UTC) by Robert Powell

Message
Thsnks for making me think about whats goin on a bit deeper, tho there are 24 hours in a day time_info.hour, 30 days to the month time_info.days and 12 months to a year time_info.year

  case 24:
      time_info.hour = 0;
      time_info.day++;
      break;
    }
    
  if(time_info.day >= 30)
    {
      time_info.day = 0;
      time_info.month++;
    }

  if(time_info.month >= 12)
    {
      time_info.month = 0;
      time_info.year++;
    } 


I now think i see why it did not execute, in update_time it sits inside the switch untill one of the conditions are met to break out, Of couse its only going to break out on the beginning of a new day, when time_info.hour hits 24.

I will try again and this time i will let it cycle through a full day, and see what happens.

Just a guy having a bit of fun. Nothing more, nothing less, I do not need I WIN to feel validated.
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #3 on Wed 13 Jul 2005 10:20 PM (UTC)
Message
I think you have the right idea but some of your vocabulary is wrong; the code doesn't 'sit around in the switch until a break'. Rather, it enters the switch, takes the appropriate branch, and leaves that branch when it hits a break.

Since the switch opens with ++time_info.hour, and case 24 sets time_info.hour back to 0, the hour never really does reach 24. Also note that those if statements are outside the switch statement.

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Raz   (32 posts)  Bio
Date Reply #4 on Thu 14 Jul 2005 02:48 PM (UTC)
Message
First, make sure that your if statement isn't in a place where it would not execute because the function has already returned.

Otherwise, add a log statement at the first line within the if statement. That will tell you for sure if it is executing. If it doesn't execute, then I would submit a bug report to your compiler's vendor.

-Raz
C++ Wiki: http://danday.homelinux.org/dan/cppwiki/index.php
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


20,770 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.