-
October 5th, 2005, 04:58 PM
#1
Bool function question
Fairly new with C++ and I have a question on checking 2 char array's to see if they match. I have the user input an 'eye' color which is eye, and then it compares it to 'e' which is read in from an external file to see if they match or not.
My other bool functions work, but this is the only one working with char arrays.
error msg: expected primary-expression before ']' token.
Here is my code.
Code:
<script c++>
bool testEye(char eye[], char e[])
{
if(strcmp(eye[],e[]))
return true;
else
return false;
}
</script c++>
-
October 5th, 2005, 05:15 PM
#2
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.
Last edited by Notsosuperhero; October 5th, 2005 at 05:22 PM.
-
October 5th, 2005, 05:21 PM
#3
Re: Bool function question
Thanks for the help.
I see what I was doing wrong.
gj
-
October 5th, 2005, 05:28 PM
#4
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.
-
October 5th, 2005, 10:11 PM
#5
Re: Bool function question
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;
-
October 5th, 2005, 11:22 PM
#6
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
-
October 5th, 2005, 11:49 PM
#7
Re: Bool function question
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.
-
October 5th, 2005, 11:57 PM
#8
Re: Bool function question
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
-
October 6th, 2005, 12:34 AM
#9
Re: Bool function question
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.
-
October 6th, 2005, 12:58 AM
#10
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.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
October 6th, 2005, 01:06 AM
#11
Re: Bool function question
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|