views:

125

answers:

5

I'm trying to compare 2 strings by "==" operator. when i print the string both are identical.

but length of one is bigger by one, i figured it is the null terminator.

any way to ignore this when comparing??

Thanks!!

City* Adjutancy::FromStringToCity(string cityName) const
{
 for (list<City*>::const_iterator it=m_citiesList.begin();it!=m_citiesList.end();it++)
  if ((*it)->GetCityName()==cityName) //this fails
   return *it;
 return NULL;
}

and

string GetCityName() const {return m_cityName;}

creation

private: 
 string m_cityName; 

and

FromStringToCity(toAdd->GetCityOfBase());

and

string Base::GetCityOfBase() const
{
 return m_baseCity;
}

and

private:
  string m_baseCity;

This is all, BTW this works on Visual Studio in windows, but now when I transfer it to Linux Kate - this where it fails.

+3  A: 

If these are string literals in quotes or character arrays, they won't compare because you're comparing the pointer addresses, not the strings themselves. If they're std::strings, it should work just fine.

If they're both std::string and one is longer than the other, we'd need to see how you're assigning the values to detect the problem.

Edit: After reading numerous comments, I believe your extra character is not a null terminator but a line-feed. You'll need to strip it off as you read the file.

Mark Ransom
when i debugged it i got the same data only one had /0 at the last index
Nadav
@Mark: the question made it clear the types involved were strings. Your edit says you've read numerous comments and concluded it might be a linefeed. Actually - as per my answer - it's probably a carriage return, and they're not the same thing.
Tony
@Tony, you might be right - which is why I already gave your answer an upvote. Certainly it's some character which is usually invisible. It seems a quirk of fate robbed you of the accepted answer: http://stackoverflow.com/questions/3765454/null-terminator-gives-trouble-with-string-length-when-comparing/3765831#3765831
Mark Ransom
+1  A: 

do

strcmp(a.c_str(), b.c_str()) == 0

this will compare them as null terminated c strings

really you should strip off the trailing NULL when you create the strings tho.

pm100
@pm100 tried this, still doesn't work
Nadav
@visionary - what the heck does that mean. I am not using == for string compare I am using strcmp. And what does reference semantics mean for C code
pm100
@nadav - then you dont have the problem you think you have. Use a debugger
pm100
@pm100 - do you really want to recommend use of strcmp (CRT) for std::string? operator== should work fine once string setup is sorted out.
Steve Townsend
@steve - he asked how to do the compare in the case where the string has trailing \0, I told him. I also told him to fix the string (so that == would work)
pm100
+2  A: 

I'd guess you're reading a file with carriage return characters. In Windows, a carriage-return / linefeed combination (ASCII 13 / 10 decimal) delimits text lines. On Linux/UNIX, only a linefeed is needed. When you read in the strings on Linux, it will retain the carriage return character in the string, rather than stripping it as it does on Windows. You need to check if the last character is a carriage return and - if so - remove it:

if (str.size() and str[str.size() - 1] == '\r')
    str.erase(str.size() - 1);

BTW / when you print (to a terminal device) a string with a carriage return, it will simply move the cursor back to the left-hand column after displaying the string. It's easier to spot if you put a quote before and after, e.g.

std::cout << "my_string \"" << my_string << "\"\n";

Then if my_string held "text\r", you'd see...

"y_string "text

...weird enough to ring an alarm bell.

Tony
A: 

@Tony, Thank you very much your solution works, I had to log off and it wouldn't let me enter my user to point the green V on, or add a comment, thanks again it was indeed the windows-linux diffrence

Nadav
Nadav, if Tony's solution works then you should Accept his answer.
Zan Lynx
@Zan, he's already admitted to having login difficulties. You can't accept the answer if StackOverflow doesn't recognize you as the OP.
Mark Ransom
@Mark, Ah, *that* green V. Got it.
Zan Lynx
@Nadav: you're welcome. Check out Jerry's post too... he's always on the ball.
Tony
+2  A: 

Now that the immediate problem you knew about has (apparently) been dealt with, let me advise that you throw away that code and replace it with something else entirely.

In particular, you seem to be using std::list to implement what's really intended to be a map -- i.e., lets you look up a city by name. The standard library already has a map (named std::map) that's more efficient and easier to use. A linked list may not be the worst possible data structure for the job at hand, but at least based on what you've shown that you're doing with it, it's certainly one of the worst choices available.

From the looks of things, you could use:

std::map<std::string, City> cityList;

or maybe:

std::map<std::string, City *> cityList;

and be a lot better off. Searches would be logarithmic instead of linear (i.e., normally quite a bit faster, especially with a large number of cities), and management would be cleaner and simpler in general (e.g., adding information about a city would be something like:

City chicago;
// fill in data about Chicago

cityList["Chicago"] = chicago;
Jerry Coffin