Re: Bool function question
make it like this:
Code:
if(strcmp(eye, e) == 0)
{
return true;
}
else
{
return false;
}
Have a look here for a better understanding of strcmp.
Re: Bool function question
Thanks for the help.
I see what I was doing wrong.
gj
Re: Bool function question
Also you could do it the C++ way.
assuming that eye and e are std::string, and not char[]
Code:
if(eye.compare(e) == 0)
return true;
else
return false;
I'm not sure that ::compare is the best way to do it with std::strings but I just began learning them.
Hope that helps.
Re: Bool function question
Quote:
Originally Posted by Notsosuperhero
Code:
if(strcmp(eye, e) == 0)
{
return true;
}
else
{
return false;
}
This can be written a little simpler:
Code:
return strcmp(eye, e) == 0;
Re: Bool function question
Better use strcmpi because it Compares two strings to determine if they are the same. The comparison is not case-sensitive.
as smasher told u write code in that way
like
return strcmpi(eye, e);
that's all no need to check for
strcmpi(eye, e)==0
Re: Bool function question
Quote:
Originally Posted by humptydumpty
return strcmpi(eye, e);
This will return incorrect results, since strcmp() and strcmpi() return zero if the two strings are equivalent, and zero indicates false. Also, the original post doesn't contain enough information to know whether using strcmpi() would be appropriate. Perhaps gjack72 wants the results to be case-sensitive.
Re: Bool function question
Quote:
Originally Posted by Smasher/Devourer
This will return incorrect results, since strcmp() and strcmpi() return zero if the two strings are equivalent, and zero indicates false. Also, the original post doesn't contain enough information to know whether using strcmpi() would be appropriate. Perhaps gjack72 wants the results to be case-sensitive.
if i used this one
strcmpi(eye, e)==0
it will return only 0 or 1
but in case of
strcmpi(eye, e)
it will return 0,1,-1 which is the return value of strcmpi,or strcmp.and i never said that u r doing anything wrong here
Re: Bool function question
Quote:
Originally Posted by humptydumpty
if i used this one
strcmpi(eye, e)==0
it will return only 0 or 1
but in case of
strcmpi(eye, e)
it will return 0,1,-1 which is the return value of strcmpi,or strcmp.and i never said that u r doing anything wrong here
Ah, well of course the return values you're talking about there are not wrong, so long as the programmer changes the way he's interpreting them. But the discussion was regarding a function that returns a bool, so the assumption thus far has been that it returns true if the strings are equivalent and false otherwise. Your way certainly works fine too, as long as you change the return type to int or something similar. But at that point, the whole testEye() function becomes useless; if the original poster wanted that kind of return value, he'd just call strcmp() or strcmpi() directly in his code, instead of wrapping the call in a function that returns a bool. ;)
Re: Bool function question
On a side note - I guess the function goes by the name stricmp and not strcmpi? Also, that function is not part of the standard. So, its better avoid it. For case-insensitive string comparisons - search in this thread - Help looping my program. Regards.
Re: Bool function question
Quote:
Originally Posted by exterminator
On a side note - I guess the function goes by the name stricmp and not strcmpi? Also, that function is not part of the standard.
I've always used stricmp() too, but Visual C++ 6.0 seems to like strcmpi(), stricmp(), and _stricmp(). Anyway, you're absolutely right that it's not standard. I should have remembered that. :)