Comparing two strings in c++ in ROS -


i trying compare 2 strings in following method on ros

std::string line_eval_str = std::string(line_eval)+""; std::string check ("checking condition");  while(/*some condtion*/) {      if(check.compare(line_eval_str) == 1)     {             ros_error("breaking loop!!!!! %s",line_eval);          break;     }     else    {           /*execute part*/    } } 

here though line_eval_str , check have same string in them else part executed. tried other method

while(/*some condtion*/) {     if(strcmp(line_eval_str,check) == 1)     {            ros_error("breaking loop!!!!! %s",line_eval);         break;     }     else     {        /*execute part*/     } } 

even code provides same result ( ie,. strings have same value else part executed). don't understand problem. have ros?

here though line_eval_str , check have same string in them else part executed

this expected behaviour: both string::compare() , strcmp() return 0 (and not 1) if strings equal. if want if() body execute, need test return value against 0:

#include <iostream>  int main() {   std::string str1 = "hello";   std::string str2 = "hello";    if (str1.compare(str2) == 0) {     std::cerr << "strings equal" << std::endl;   } else {     std::cerr << "strings not equal" << std::endl;   }    return 0; } 
$ g++ -o cmp cmp.c $ ./cmp strings equal 

for better readability, should use comparison operator instead of compare method:

if (str1 == str2) { ... 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -