Hi,
maybe someone could help me understand what is wrong here.
Below is a simple multimap studsubj, and I compare word_1,word_2 word_3 with it. I give these words the values exactly as they are in
studsubj, then compare. For example, if word_1 = "we" ( or "you" ) ,word_2 = "WORK", word_3 = "today" the match with studsubj is perfect.
The problem is lines starting with "THEY". To be more exact, the first line is ok, but comparing with any of the other two lines only has match with word_1, but not word_2 and word_3. Why?
Thank you very much.
Code:
#include <iostream>
#include <map>

#include <string>
using namespace std;

class student
{
private:
    string element1;
    string element2;
    string element3;
    
public:
    student( ) { element1 = element2 = element3 = " ";     }                      // default constructor
    
    student(string a,string b,string c):element1(a),element2(b),element3(c) {} // all elements filled
    student(string a):element1(a) { element2 =  element3 = " "; }             //  to compare  element1
    
    string e_1( ) {  return element1; }
    string e_2( ) {  return element2; }
    string e_3( ) {  return element3; }
};

bool operator < (student a,student b) {     return a.e_1() < b.e_1(); }

class subject
{
private:
    string option;

    
public:
    subject( )     { option =" "; }
    
    subject(string a):option(a) {}
    
    string str( ) { return option;}
    
};
typedef multimap<student,subject,less<student> > mapsub;


int main( )
{
    typedef multimap<student,subject,less<student> > mapsub;
    mapsub studsubj;
    mapsub::const_iterator ptr;
    
    /* 1 */  studsubj.insert(pair<student,subject>(student("we","WORK","today"), subject("option 3")));     
    /* 2 */  studsubj.insert(pair<student,subject>(student("you","WORK","today"), subject("option 4")));   
        
    /* 3 */  studsubj.insert(pair<student,subject>(student("THEY","do","work"), subject("option 1")));
    /* 4 */  studsubj.insert(pair<student,subject>(student("THEY","read","books"), subject("option 1")));
    /* 5 */  studsubj.insert(pair<student,subject>(student("THEY","work","today"), subject("option 1")));
    
    
    
    string word_1="THEY",word_2 = "read",word_3 ="books"; //  input words to сомраre with  words from multimap studsubj
    
    student st(word_1);
    ptr = studsubj.find(st);
    if(ptr != studsubj.end())
    {
        
        
        student st= ptr->first;
        cout<<"st.e_1() match word_1 ( "<<word_1<<" )"<<endl;
        do
        {
            
            if(st.e_2() == word_2 )
            {
                cout<<"st.e_2() match word_2 ( "<<word_2<<" )"<<endl;    
                    
            
            if( st.e_3() == word_3 )
            {
        
                cout<<"st.e_3() match word_3 ( "<<word_3<<" )"<<endl;        
                break;
            }
                    
            else 
            {
        
                cout<<endl<<"word3 doesn't match word_3 ( "<<word_3<<" )"<<endl;    
                break;
            }
                
            } //  if(st.e2() == word2 )
            else 
            {
                cout<<"st.e_2() doesn't match word_2 ( "<<word_2<<" )"<<endl;    
                break;
            }
            
            
        ptr++;    
        }
        while(ptr != studsubj.upper_bound(word_2));
    }    
    
    return 0;
}

Last edited by shabbir; 18Sep2011 at 08:28.. Reason: Code blocks