1

The code below has alot of outputting strings at the end i try to push the back onto avector and the append it to a string so then i can return it, but it only gets the last string which is outputted i need to get all of them.

What am i doign wrong so that i can push back all the strings

DCS_LOG_DEBUG("--------------- Validating .X/ ---------------")
std::string str = el[i].substr(3);
std::vector<std::string>st;
split(st,str,boost::is_any_of("/"));
boost::regex const string_matcher(splitMask[0]);
if(boost::regex_match(st[0],string_matcher))
{
    a = "Correct Security Instruction\n";
}
else
{
    a = "Incorrect Security Instruction\n"
}


boost::regex const string_matcher4(splitMask[4]);
if(boost::regex_match(st[4],string_matcher4))
{
    a = "Correct Autograpgh\n"
}
else
{
    a = "Incorrect Autograpgh\n"
}

boost::regex const string_matcher5(splitMask[5]);
if(boost::regex_match(st[5],string_matcher5))
{
    a = "Correct Free text\n";

}
else
{
    a = "Incorrect Free text\n"
}

std::vector<std::string>::iterator it;
std::string s = ("");
output.push_back(a);
i++;

for(it = output.begin(); it < output.end(); it++)
{
    s+= *it;
}

return s;
sehe
  • 374,641
  • 47
  • 450
  • 633
CodersSC
  • 710
  • 2
  • 13
  • 29

2 Answers2

1

Assigning more than once to a will replace, not concatenate. What you are looking for, is more likely output streaming (or output iterators).

Propose to simplify:

DCS_LOG_DEBUG("--------------- Validating .X/ ---------------")
std::string str = el[i].substr(3);
std::vector<std::string> st;
split(st,str,boost::is_any_of("/"));
boost::regex const string_matcher(splitMask[0]);
boost::regex const string_matcher4(splitMask[4]);
boost::regex const string_matcher5(splitMask[5]);

std::ostringstream oss;

oss << (boost::regex_match(st[0],string_matcher )? "correct":"incorrect") << " Security Instruction\n";
oss << (boost::regex_match(st[4],string_matcher4)? "correct":"incorrect") << " Autograpgh\n";
oss << (boost::regex_match(st[5],string_matcher5)? "correct":"incorrect") << " Free text\n";

return oss.str();

Include <sstream> for std::ostringstream

sehe
  • 374,641
  • 47
  • 450
  • 633
0

Are you sure that the result you are getting is not just the first string?

That said, it is not entirely clear what you are trying to do, but assuming that there is some looping code above what you posted it seems that your problem is with the positioning of the for loop.

         while( i < el.size() )  //Assuming something like this
         {                  
              ...

              else
              {
                  a = "Incorrect Free text\n"
              }               
              output.push_back(a);
              i++;
          }
          //move this stuff out of the loop so that it only runs after you have 
          // processed all the strings in el
          std::vector<std::string>::iterator it;
          std::string s = ("");
          for(it = output.begin(); it < output.end(); it++)
          {
             s+= *it;
          }
          return s;
       }
samedi81
  • 46
  • 1
  • 3