it's part of a function that's supposed to do a word search in a binary file.
the first if searches for the singular form of the word. if it doesn't find it goes to second if searches for plural form. again, if it doesn't find it goes to the third if the didyoumean search type. all good.
the problem is that if the first if works it still does the third if . same goes for the second if.
for the first and second if the output should only be the word's translation but i also get the some other words coresponding to the third if.
the first if searches for the singular form of the word. if it doesn't find it goes to second if searches for plural form. again, if it doesn't find it goes to the third if the didyoumean search type. all good.
the problem is that if the first if works it still does the third if . same goes for the second if.
for the first and second if the output should only be the word's translation but i also get the some other words coresponding to the third if.
any ideas?
thanks
Reformat your code, and you will see what you're saying cannot possibly happen:
What doesn't work? The if() statements do "work", in the sense that if the first if() is processed, none of the other conditions are processed.
theoretically it should work .. but it still doesn't .. it has something to do with the strncmp line. i've changed it with something else and it works how it should. how should i write the strncmp line so i don't get this "bug" ?
OP, I hope you can understand how frustrating of a thread this is. Please post a complete example that demonstrates the problem. For all we know the code that you pasted isn't the same as what you are executing. Your description of the original problem makes no sense at all. Paul already pointed that out yet you have not really answered his question. By the way I cannot open the URLs as they are blocked by my company firewall. It is best if you post a program that others can compile and execute on their own in order to duplicate the problem.
Last edited by kempofighter; January 18th, 2010 at 05:19 PM.
In addition to what the other posters have asked for or said, put a couple of break points in each of the if, ifelse, else clauses and step through the code in a debugger.
If you are relying on the console output, then are you sure you aren't confusing the output?
My curiousity got the best of me so here is the output from my execution of the program.
Code:
singular : light
plural : lights
definition: provides the ability to see
light,lights(pl) - provides the ability to see
Search : light
- - - - - - - - - -
light, (pl) - provides the ability to see
- - - - - - - - - -
light, (pl) - provides the ability to see
- - - - - - - - - -
light, (pl) - provides the ability to see
EDITED to clarify the output.
The first output was echoed during the add function. The next three were due to the word light being found multiple times within the array. Somehow entries 7,8,9, and 10 all contain the user input. The problem is not the if..elseif..else statements at all. The problem seems to be duplicate entries in the searchable array.
Last edited by kempofighter; January 19th, 2010 at 05:11 PM.
My curiousity got the best of me so here is the output from my execution of the program.
Code:
singular : light
plural : lights
definition: provides the ability to see
light,lights(pl) - provides the ability to see
Search : light
- - - - - - - - - -
light, (pl) - provides the ability to see
- - - - - - - - - -
light, (pl) - provides the ability to see
The first output was echoed during the add function. The last was output during the search function. The program seems to have worked as advertised for me. I didn't see any issues with the strncmp call not working or the code executing within multiple if..elseif blocks. I'm only looking for the problem originally described by the OP and am not going to critique the code in any way. So for what it's worth....
ok. then i must have asked the wrong question. i'm going to reformulate.
why, when i use "Search: " i get answers from other ifs instead of getting only from one if.
ok. then i must have asked the wrong question. i'm going to reformulate.
why, when i use "Search: " i get answers from other ifs instead of getting only from one if.
and please, do critique the code.
sry if i'm confusing .. i'm sort of a begginer
tnx
There are only two possibilities:
1) Your if statements end up comparing the same thing.
2) You have redundant entries in the data files.
Given the data, are you sure that these statements evaluate uniquely?
Code:
if ( strcmp ( a[i].word, search_word ) == 0 )
if ( strcmp (a[i].word_pl, search_word ) == 0 )
if ( strnicmp ( a[i].word, search_word,2 ) == 0 )
If you see duplicate entries, why don't you print out the file and line number, so when you see a duplicate, you can see the data that caused it to happen?
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.