views:

71

answers:

1

my code works perfectly in visual studio yet i encounter a problem running it in eclipse. in the function:

City* Gps::FindCity(const char* city)  
 {  
 if(city != NULL)
 {  
      City *tmp = NULL;  
      if (! m_gpsCities.empty())  
      {  
           for (list<City*>::iterator iter = m_gpsCities.begin(); iter != m_gpsCities.end(); iter++) 
           {  
                tmp = (City*)(*iter);  
                if(Vehicle::StringCompare(tmp->GetCityName(),city)==0)  
                return tmp;    
           }  
      }  
 }  
 return NULL;  
}  

the problem is, that after the first iteration, and while the list has more then 1 elements, it exits the loop, and doesn't go over the other elements in the list.

stringcompare:

 int Vehicle::StringCompare(const char* str1, const char* str2)//assuming all not null  
 {  
    string s1, s2;  
    char *st1 = OrgName(str1),*st2 = OrgName(str2);  

    s1.assign(st1);  
    s2.assign(st2);  

    int size1 = s1.size(), size2 = s2.size(), min = 0, index =0;  

    if(str1[size1 - 1] == '\r' || str1[size1 - 1] == '\0' || str1[size1 - 1] == '\n')  
        size1--;  
    if(str2[size2 - 1] == '\r' || str2[size2 - 1] == '\0' || str2[size2 - 1] == '\n')  
        size2--;  

    if(size1>size2)  
        min=size2;  
    else  
        min=size1;  

    bool bigger1 = true;  
    for(index=0;index<min;index++)  
    {  
        if(st1[index]>st2[index])  
            return 1;  
        if(st1[index]<st2[index])  
            return (-1);  
    }  

    delete[] st1;  
    delete[] st2;  

    if(size1==size2)  
        return 0;  
    if(min==size1)  
       return (-1);  
    else  
        return 1;  
}
+1  A: 

You just want to achieve find_if for your specific predicate, which is indeed a variant of strcmp with less specific integer output. Try this:

City* Gps::FindCity(const char* MyCityName)  
{  
    if((MyCityName!= NULL)&&(!m_gpsCities.empty())
    {  
        for (list<City*>::const_iterator iter=m_gpsCities.begin(); iter!=m_gpsCities.end(); ++iter)
        {  
            const char* MyCityTempNameChar = iter->GetCityName();
            const char * st1 = OrgName(MyCityName),
            const char * st2 = OrgName(MyCityTempName);  
            const int predicate = strcmp(st1, st2);
            if(predicate==0)
            {  
                return (*iter);  
            }  
        }  
    }  
    return NULL;  
}

Or adapt the predicate according to the following:

 int Vehicle::StringCompare(const char* str1, const char* str2)
 {  
    const char * st1 = OrgName(str1),
    const char * st2 = OrgName(str2);  
    const int predicate = strcmp(st1, st2);
    return predicate; 
 }
wok
but i need to use my comparison, i compare it in a certain way.
shiran bar
It seems you just compare two city names.
wok
i do it in a specific way that was requested
shiran bar