Function Always Returns Zero

I have this function that I tested outside the current solution and it worked fine. When I moved it into the new solution and changed it to search data which is an unsigned char. The function stopped returning the correct value. Also believe it or not I was getting four seperate correct answers on the returns!

[code]#include <string>
#include <iostream>

int Count( const std::string & str,
const std::string & obj ) {
int a = 0;
int b = 0;
int c = 0;
int d = 0;
std::string ::size_type pos = 0;
while( (pos = obj.find( str, pos ))
!= std::string::npos ) {
a++;
b++;
c++;
d++;
pos += str.size();
}
return a;
return b;
return c;
return d;
}
void printTcpContent(unsigned char *data,int)
{
std::string s = (const char*) data;
int a = Count( "text/html", s );
int b = Count( "text/plain", s );
int c = Count( "image/jpg", s );
int d = Count( "image/png", s );
std::cout << a << std::endl;
std::cout << b << std::endl;
std::cout << c << std::endl;
std::cout << d << std::endl;
}
Last edited on
1
2
3
4
return a;
return b;
return c;
return d; 

So, uh, what did you think the above would do?

Also believe it or not I was getting four seperate correct answers on the returns!

What?

Also, your current solution only works if there are no null bytes in your data.
1
2
3
4
return a;
return b;
return c;
return d; 


Does that compile without a warning? Lol why does it even compile at all?
Whenever you hit a return, you exit that function. Or so you should.
For whatever reason it works. Now for the problem at hand......
void printTcpContent(unsigned char *data,int)
{
std::string s = (const char*) data;
This is the problem. data is an unsigned char and std::string doesn't reconize it. What to do?
Please use [code]// code here [/code] tags.

Back on topic, could you give us the exact error that the compiler gives you?
Also std::string s = (char*) data; should work fine.
Although you should use the more C++-like std::string s = reinterpret_cast<char *> (data); instead.
Last edited on
Topic archived. No new replies allowed.