Hi,I have trouble comparing two strings with strcmp. The bold part of the code are the parts that are not working and i hope somebody can explain to me what i did wrong. Goal of the code is to compare a city name with the name of already created cities and when two of them match to creat a Street between them. Depending on the street pointer different streets can be created
my vector containing all already created cities (it seem to work) std::vector<city*> citylist;//all cities
find city using strings and creating a road between them:
bool Map::add_street(Street *street, const string firsttown , const string secondtown)
{
city* hilfs1=Map::find_city(erste);
city* hilfs2=Map::find_city( zweite);
if (hilfs1==NULL || hilfs2==NULL)
{returnfalse ;} // Problem :both pointers are always NULL
else
{
street=new Street(hilfs1,hilfs2);//creating a new street with two pointer as parameters
hilfs1->add_street(street);
hilfs2->add_street(street);// adds the street to the streetlist which contain all roads that are connected with this city
returntrue;
}
}
city* Map::find_city(const string blub)
{
typedef vector <city*>:: iterator iterTyp ;
cout << citylist.size();
for ( iterTyp it = citylist.begin(); it != citylist.end (); it ++)
{
if (strcmp(((*it)->getname()).c_str(),blub.c_str())==0)// always wrong
{
return (*it);
cout<<"Found city";// does not show in console
break;
}
else
{
return NULL;
cout<<"NOT FOUND city";// does not show in console
break;
}
}
}
Here is also my implementation of the getname function :
Thank you ne555 for you answer which helped me a lot and of course thank you for taking your time to help me.
Iam amazed that i missed the both of these mistakes.