Reputation: 1
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.
Upvotes: 0
Views: 1152
Reputation: 106236
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.
Upvotes: 2
Reputation: 490573
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;
Upvotes: 2
Reputation: 308500
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::string
s, 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.
Upvotes: 3
Reputation: 50210
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.
Upvotes: 1