problem with comparing two strings

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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)
 {return false ;} // 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
 return true;
}
}

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 :
1
2
3
4
std::string city::getname()
{
return this->name;
}
Last edited on
if( (*it)->getname() == blub )


1
2
      return (*it);
      cout<<"Found city";// does not show in console 
of course not, you just terminated the function in the above line

Also, if you bother to indent your code, you may notice that you've got
1
2
3
4
5
6
7
8
for(){
   if(){
      return
   }
   else{
      return
   }
}
so you only consider the first element of the vector.
Last edited on
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.
Topic archived. No new replies allowed.