for/if/else dilemma
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 19

Thread: for/if/else dilemma

Hybrid View

  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,423

    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,423

    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 04:19 PM.

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

    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 / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,197

    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 / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,197

    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
    Jan 2010
    Posts
    8

    Re: for/if/else dilemma

    so.. i found out what the real problem is. i thought that after doing first or second if the search jumps to the third if but actualy after doing first or second if it runs the for again, thus doing the third if and i get both solutions. if i added 2-3 more ifs that matched the search_word it would, after every if matb]ched, jump again to the for until there aren't any other ifs matching the search_word.

    any ideea why it's doing this?

    thanks for your solutions so far

  14. #14
    Join Date
    Apr 1999
    Posts
    27,423

    Re: for/if/else dilemma

    Quote Originally Posted by darkseid View Post
    any ideea why it's doing this?
    That's how for() loops work.
    Code:
    for(i=0; i<n; i++)
    This will loop n times, unless a break statement stops the loop.

    Regards,

    Paul McKenzie

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center