| View previous topic :: View next topic |
| Author |
Message |
AIva1110 Advanced Cheater
Reputation: 0
Joined: 16 Sep 2009 Posts: 56 Location: The Void
|
Posted: Wed Sep 23, 2009 6:19 am Post subject: StrCmp |
|
|
if(!strcmp(flight[t].from, from) && !strcmp(flight[t].to, to))
Anybody knows why statements in if are not returning true in this statement, since all parameters are correct and checked before execution.
Strcmp returns 0, in c anything but 0 is true, that is why ! statements are used. _________________
|
|
| Back to top |
|
 |
Slugsnack Grandmaster Cheater Supreme
Reputation: 71
Joined: 24 Jan 2007 Posts: 1857
|
Posted: Wed Sep 23, 2009 6:26 am Post subject: |
|
|
| we can't check until you give us more code. i suggest you debug it. the computer can not be wrong. if it's returning a result you are not expecting it must be an error on your side |
|
| Back to top |
|
 |
AIva1110 Advanced Cheater
Reputation: 0
Joined: 16 Sep 2009 Posts: 56 Location: The Void
|
Posted: Wed Sep 23, 2009 6:46 am Post subject: |
|
|
Problem is im in library atm so i cant use olly here, there isnt much point in posting anything else because functions are independent, only 2 parameters are passed to this function declaration is : int match(char *from,char *to)
after that i check parameters with printf() to see if they were passed correctly, they are. After this follows if statement. Here, main part of the function checks excluded. I checked what's t and what is passed from array as well.
| Code: |
int match(char *from,char *to)
{
register int t;
for(t=f_pos-1; t>-1; t--)
{
if(!(strcmp(flight[t].from, from) && strcmp(flight[t].to, to)))
{
return flight[t].distance;
}
} return 0; /*Not found*/
}
|
_________________
|
|
| Back to top |
|
 |
Slugsnack Grandmaster Cheater Supreme
Reputation: 71
Joined: 24 Jan 2007 Posts: 1857
|
Posted: Wed Sep 23, 2009 7:10 am Post subject: |
|
|
okay before the strcmps, add a MessageBox which display all 4 compared strings. I'm guessing what's happening is that your match function is returning 0 ?
i'm also guessing 'flight' is extern, i don't see it anywhere in your function ? |
|
| Back to top |
|
 |
AIva1110 Advanced Cheater
Reputation: 0
Joined: 16 Sep 2009 Posts: 56 Location: The Void
|
Posted: Wed Sep 23, 2009 7:22 am Post subject: |
|
|
Flight is an array of structures, I'm comparing it's member .from with parameter "from" which is passed to function. I checked everything that's being compared, it prints correct strings. Now I tried printing return value of StrCmp and it just crashes the program, probably because I don't know how to print it lol. Any ideas how to print return value correctly? google search didn't yield much results. _________________
|
|
| Back to top |
|
 |
Slugsnack Grandmaster Cheater Supreme
Reputation: 71
Joined: 24 Jan 2007 Posts: 1857
|
Posted: Wed Sep 23, 2009 7:37 am Post subject: |
|
|
| Code: | | printf ("Return was %d\n", result); |
or wcstol() |
|
| Back to top |
|
 |
Deltron Z Expert Cheater
Reputation: 1
Joined: 14 Jun 2009 Posts: 164
|
Posted: Wed Sep 23, 2009 7:39 am Post subject: |
|
|
What if one of the string returns a non-zero value? which means, the strings are different and the result of x & 0 = 0, then when you NOT it then you'll get 1 even though only one of the strings is different than the other.
You should do:
if (strcmp(..) == 0 && strcmp(..) == 0)
...... |
|
| Back to top |
|
 |
Slugsnack Grandmaster Cheater Supreme
Reputation: 71
Joined: 24 Jan 2007 Posts: 1857
|
Posted: Wed Sep 23, 2009 7:54 am Post subject: |
|
|
| yes, his code in the first post was correct. i did not notice that it was different in the code example he gave though |
|
| Back to top |
|
 |
AIva1110 Advanced Cheater
Reputation: 0
Joined: 16 Sep 2009 Posts: 56 Location: The Void
|
Posted: Wed Sep 23, 2009 8:26 am Post subject: |
|
|
That code i gave has a mistake i left it doing experiments, assume code from first post is used.
I tried testing return value of strcmp using printf() as slugsnack suggested and it returned wrong result, then I checked flight[t].from against from passed to a function and flight[t].from has string from the bottom of database when it was supposed to have it from the top, the odd thing is that top and bottom entries match and i didn't notice this until now. When checking second string program crashed because they were incorrect...now it's fixed. Thanks for help everyone. _________________
|
|
| Back to top |
|
 |
|