CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Sep 2005
    Location
    Colorado, USA
    Posts
    86

    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++>

  2. #2
    Join Date
    Dec 2003
    Location
    Syracuse, NY
    Posts
    400

    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.
    "Windows programming is like going to the dentist: You know it's good for you, but no one likes doing it."
    - Andre LaMothe

    DLL For Beginners(UPDATED)
    Please use CODE tags
    Rate me if I've helped.

  3. #3
    Join Date
    Sep 2005
    Location
    Colorado, USA
    Posts
    86

    Thumbs up Re: Bool function question

    Thanks for the help.

    I see what I was doing wrong.

    gj

  4. #4
    Join Date
    Dec 2003
    Location
    Syracuse, NY
    Posts
    400

    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.
    "Windows programming is like going to the dentist: You know it's good for you, but no one likes doing it."
    - Andre LaMothe

    DLL For Beginners(UPDATED)
    Please use CODE tags
    Rate me if I've helped.

  5. #5
    Join Date
    May 2005
    Location
    United States
    Posts
    526

    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;

  6. #6
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    Lightbulb 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

  7. #7
    Join Date
    May 2005
    Location
    United States
    Posts
    526

    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.

  8. #8
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    Lightbulb 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

  9. #9
    Join Date
    May 2005
    Location
    United States
    Posts
    526

    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.

  10. #10
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Thumbs up 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.

  11. #11
    Join Date
    May 2005
    Location
    United States
    Posts
    526

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured