CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    Join Date
    Jan 2010
    Posts
    8

    Question for/if/else dilemma

    Code:
     for(i=0; i<n; i++)
            if ( strcmp ( a[i].word, search_word ) == 0 ) {   // (1)
    
                cout << "- - - - - - - - - -" << endl;
                cout << a[i].word << ", " << a[i].word_pl << "(pl)" << " - " << a[i].word2 << endl;
    
            }
    
    
            else if ( strcmp (a[i].word1_pl, search_word ) == 0 ){  // (2)
    
                cout << "- - - - - - - - - -" << endl;
                cout << a[i].word_pl << ", " << a[i].word<< "(sg)" << " - " << a[i].word2 << endl;
    
                }
    
    
    
    
            // " did you mean ?" search
    
            else if ( strncmp ( a[i].word1, search_word, 2) == 0){  // (3)
                    cout << a[i].word1 << endl;
    
    
            }
    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.

    any ideas?

    thanks

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: for/if/else dilemma

    Quote Originally Posted by darkseid View Post
    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:
    Code:
    for(i=0; i<n; i++)
            if ( strcmp ( a[i].word, search_word ) == 0 ) 
            {
                cout << "- - - - - - - - - -" << endl;
                cout << a[i].word << ", " << a[i].word_pl << "(pl)" << " - " << a[i].word2 << endl;
            }
    
            else 
            if ( strcmp (a[i].word1_pl, search_word ) == 0 )
            {
                cout << "- - - - - - - - - -" << endl;
                cout << a[i].word_pl << ", " << a[i].word<< "(sg)" << " - " << a[i].word2 << endl;
             }
    
             else 
             if ( strncmp ( a[i].word1, search_word, 2) == 0)
             {
                    cout << a[i].word1 << endl;
             }
    I see no way what you're saying could happen. Once the first if() condition is met, the else-if's cannot occur, unless your compiler is broken.

    Also, I recommend bracketing the for() statement itself.
    Code:
    for ( whatever )
    {
    //...
    }
    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Jan 2010
    Posts
    8

    Re: for/if/else dilemma

    thanks for the advice. but still doesn't work properly.

    here's the output for first if working :



    and second if working


  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: for/if/else dilemma

    Quote Originally Posted by darkseid View Post
    thanks for the advice. but still doesn't work properly.
    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.

    Whether the processing is correct, that is a different story altogether.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Jan 2010
    Posts
    8

    Re: for/if/else dilemma

    Quote Originally Posted by Paul McKenzie View Post
    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" ?

    thanks

  6. #6
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Lightbulb Re: for/if/else dilemma

    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.

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: for/if/else dilemma

    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?

    This output
    Code:
    cout << a[i].word << ", " << a[i].word_pl << "(pl)" << " - " << a[i].word2 << endl;
    is very similar to
    Code:
     
    cout << a[i].word_pl << ", " << a[i].word<< "(sg)" << " - " << a[i].word2 << endl;
    if you can't step through the code in a debugger, help yourself out by changing the output to:
    Code:
    cout << a[i].word << ", " << a[i].word_pl << "(pl)" << " - " << a[i].word2 << endl;
    cout << "Condition 1" << endl;
    
    and
    Code:
     
    cout << a[i].word_pl << ", " << a[i].word<< "(sg)" << " - " << a[i].word2 << endl;
    cout << "Condition 2" << endl;
    

  8. #8
    Join Date
    Jan 2010
    Posts
    8

    Re: for/if/else dilemma

    sorry for the confusion. here is the full code

    main.cpp
    Code:
    #include <iostream>
    #include "class.h"
    using namespace std;
    int main()
    {
    
                translator T;
    
                T.add();
    
                T.search();
    
    }
    class.cpp
    Code:
    #include <iostream>
    #include <fstream>
    #include "class.h"
    #include <string.h>
    
    using namespace std;
    translator a[100];
    int n;
    int m;
    int x;
    
    
    void translator::add()
    {
    	ofstream iFile("singular.bin", ios::binary | ios::app);
    	ofstream dFile("definition.bin", ios::binary | ios::app);
    	ofstream pFile("plural.bin", ios::binary | ios::app);
    	ifstream gFile("nr.bin", ios::binary);
    	gFile >> n;
    	cout << endl;
    	cout << "singular : ";
    	cin.getline (word,100);
    	cout << "plural : ";
    	cin.getline (word_pl,100);
    	cout << "definition: " ;
    	cin.getline (word2,100);
    	cout << word << "," << word_pl << "(pl)" <<" - " << word2 << endl;
    	iFile << word << endl;
    	pFile << word_pl << endl;
    	dFile << word2 << endl;
    	ofstream jFile("nr.bin",ios::binary);
    	n++;
    	jFile << n;
    	iFile.close();
    	pFile.close();
    	dFile.close();
    	jFile.close();
    	gFile.close();
    	}
    
    void translator::search()
    {
       int i=0;
       char *search_word;
       search_word = new char[30];
    
       cout << "Search : ";
       cin >> search_word;
    
       ifstream iFile("singular.bin", ios::binary);
       ifstream pFile("plural.bin", ios::binary);
       ifstream dFile("definition.bin", ios::binary);
       ifstream jFile("nr.bin", ios::binary);
        jFile >> n;
    
       if (iFile.fail() )
       {
           cout << endl << "Error" << iFile << endl;
       }
       else {
    	for(i=0;i<n;i++)
           iFile.getline (a[i].word, 100);
       }
    
       if (dFile.fail() )
       {
           cout << endl << "Error" << dFile << endl;
       }
           else {
               for (i=0;i<n;i++)
                dFile.getline (a[i].word2,100);
           }
    
    
        if (pFile.fail() )
        {
            cout << endl << "Error" << pFile << endl;
        }
            else {
                for (i=0;i<m;i++)
                 pFile.getline(a[i].word_pl,100);
            }
    
    
    
            for(i=0; i<n; i++) {
    
            if ( strcmp ( a[i].word, search_word ) == 0 ) {
                 cout << "- - - - - - - - - -" << endl;
                cout << a[i].word << ", " << a[i].word_pl << "(pl)" << " - " << a[i].word2 << endl;
    
            }
    
    
    
            else
            if ( strcmp (a[i].word_pl, search_word ) == 0 ){
               cout << "- - - - - - - - - -" << endl;
                cout << a[i].word_pl << ", " << a[i].word<< "(sg)" << " - " << a[i].word2 << endl;
    
            }
    
    
    
    
            else
            if ( strnicmp ( a[i].word, search_word,2 ) == 0){
                cout << a[i].word << endl;
    
            }
            }
    
       delete search_word;
       iFile.close();
       pFile.close();
       dFile.close();
       jFile.close();
    
    }
    class.h
    Code:
    #ifndef classh
    #define classh
    
    class word {
    protected:
    	char word[100];
    	char word_pl[100];
        char word2[100];
    };
    
    
    class translator : protected word {
    public:
    	void add();
    	void search();
    
    };
    
    
    #endif

  9. #9
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: for/if/else dilemma

    How about zipping up a small project and include some binary files.

    I can't step through the code without the files you're are loading.

  10. #10
    Join Date
    Jan 2010
    Posts
    8

    Re: for/if/else dilemma

    Quote Originally Posted by Arjay View Post
    How about zipping up a small project and include some binary files.

    I can't step through the code without the files you're are loading.
    here you go

    translator.zip - 0.00MB

    i hope zshare is alright.

    tnx for the interest

  11. #11
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: for/if/else dilemma

    Instead of zshare, how about just attaching the zip file directly here? [click on the manage attachments button]

  12. #12
    Join Date
    Jan 2010
    Posts
    8

    Re: for/if/else dilemma

    i guess it's better that way :P
    Attached Files Attached Files

  13. #13
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Re: for/if/else dilemma

    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.

  14. #14
    Join Date
    Jan 2010
    Posts
    8

    Re: for/if/else dilemma

    Quote Originally Posted by kempofighter View Post
    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.

    and please, do critique the code.

    sry if i'm confusing .. i'm sort of a begginer

    tnx

  15. #15
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: for/if/else dilemma

    Quote Originally Posted by darkseid View Post
    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?

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured