Link to home
Start Free TrialLog in
Avatar of LeighWardle
LeighWardleFlag for Australia

asked on

My C/C++ if statement is not working as planned

Hi Experts,

Here's my code:

int myRecon(char *server,int retrycount, int retries)
{
    int retriesremaining;
   int minutesremaining;
   char buffer[1024];
   
   retriesremaining = retries - retrycount;
   minutesremaining = 2*retriesremaining;


//   skip message if retries > retrycount
   if (retries > retrycount)
   {
   
   sprintf(buffer, "Lost connection with the Licence Server.\nIt's most likely your Internet connection is broken.\nAttempting to reconnect every 2 minutes.\nIf the connection is not working in %d minutes, CIRCLY will automatically close, so the Licence can be returned to the Licence Server.\n\nretrycount=%d, retries=%d", minutesremaining,retrycount,retries);
 
   
   MessageBox(
        NULL,
        buffer,
        "Connection Status",
      MB_OK
    );
   
   }
   return(0);
}   

Open in new window


My problem is that when retries = 2 and retrycount = 2, the  if (retries > retrycount) statement executes as True, but it should be False.

Regards,
Leigh

ASKER CERTIFIED SOLUTION
Avatar of phoffric
phoffric

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of LeighWardle

ASKER

Here's the output from within the if statement, showing retries = 2 and retrycount = 2:

User generated image
Avatar of phoffric
phoffric

I do not use MessageBox in my programs.

Could you modify this function by printing to the console instead of MessageBox; and write a simple main function to call it. Then post your mini program.

Then tomorrow I will run it in visual studio.

I still recommend that you perform the three debugging steps I mentioned. Talk to you in 13 hours.
Hi LeighWardle,

this sounds very strange. It's very unlikely such things happen, it's essential those if's work correct.

Is the above code really exactly that one you're using? You're sure there's no accidentally wrong ; or a \ in the code which is not pasted here?

Just for interest: could you show the value of retriesremaining?

It should be 0, if both values are equal. In fact (because a > b == ( a - b ) > 0) the if-statement could be replaced with
if ( retriesremaining > 0 )

Open in new window

It would be interesting if this even results to true if retries and retrycount are equal.

Best regards,

ZOPPO
Just a wild guess (from my own experience)... You may use the code more times on different places. You may actually observe the message from another part of the program. Firstly, search the "Lost connection with the Licence Server.... everywhere in your project (Ctrl+Shift+F) to be sure.
Well, I would modify the code and thus enforce an recompilation and linking, not that you're using an old version. E.g.

int myRecon(char *server, int retrycount, int retries)
{
    int minutesremaining;
    char buffer[1024];
    if (retries > retrycount)
    {
        minutesremaining = 2 * (retries - retrycount);
        sprintf(buffer, "Lost connection with the Licence Server. Remaining %d minutes, retrycount=%d, retries=%d", minutesremaining, retrycount, retries);
        MessageBox(NULL, buffer, "Connection Status", MB_OK);
    }

    return(0);
}

Open in new window

The only problem with such a comparison arises when using floats, but this is not the case.
Instead of printing out variables, the best thing to do is to use a debugger, toggle breakpoints, and watch variables.
Hi Everyone who has commented,

Some important background to this code:
It is being used to create a .dll and my app registers the callback functions that are in the code.
For weeks I’ve been updating the code and using the .dll without issue.
But today I made a major change to the code – it compiles without error and the .dll has an up to date datestamp.
But when I run my app I see the output from an older version of the code.
This would explain the "spurious" behaviour of the if statement.
In fact, I am seeing output from code that does not include that if statement.
I’ve rebooted to no avail.

Is there some way to clear the old version of the .dll?

Regards,
Leigh
The dll is registered in the Windows registry. If you do not find any better tool/reason, use the regedit to find the dll, and use regsvr32 to unregister the old and register the new version of the dll.
Thanks, pepr, for the suggestion.
I searched through the registry - there are no relevant references to the dll.
Hi Everyone who has commented,

It just dawned on me that I have had a Brain Snap.

I had overlooked copying the .dll from the folder where it was created to the folder I run my app from.

Sorry for you having spent time thinking about my "if statement problem".

I will award points shortly...

Regards,

Leigh


Hi Everyone who has commented,

Thanks for all your suggestions.

As stated above, my Brain Snap meant this post has been a red herring.

Regards,

Leigh


No problem about the brain snap. It happens more than we all like to admit. By confirming that the if-code is good, that enables you to look for a more exotic solution, or just a plain old copy. :)

I fully expected you to get very close to the actual issue after a little debugging.
hmm..

[..] not that you're using an old version.
;)