Thread: strcmp help

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    77

    strcmp help

    if(!strcmp(m_edit,"haha");
    {
    MessageBox("haha");
    }
    else
    {
    MessageBox("LOL");
    }

    is there anything wrong with this code???
    how come my result is the "LOL" one even i enter the correct one
    im using the VisualC++

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    strcmp() is telling you that the two strings are not the same. Since one string is a literal, I would suggest you check the contents of the other, because it is clearly not what you think it is!
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    Registered User WayTooHigh's Avatar
    Join Date
    Aug 2001
    Posts
    101
    and strcmp() is case sensitive. so 'm_edit' must contain 'haha', not 'Haha' or 'HAHA' and so on...
    Sometimes, the farthest point from the center is the center itself.

    Your life is your canvas, it's only as beautiful as you paint it.

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    77
    can i make it not case sensitive

  5. #5
    Registered User (TNT)'s Avatar
    Join Date
    Aug 2001
    Location
    UK
    Posts
    339
    Hey,

    Yer you could convert the string to lowercase before you check it.(the one in the edit box).

    Look into the function tolower() and use it before you do the strcmp.



    Hope that helps.
    TNT
    TNT
    You Can Stop Me, But You Cant Stop Us All

  6. #6
    Registered User WayTooHigh's Avatar
    Join Date
    Aug 2001
    Posts
    101
    can i make it not case sensitive
    yes, use strcmpi().
    Sometimes, the farthest point from the center is the center itself.

    Your life is your canvas, it's only as beautiful as you paint it.

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    77
    how to i use the strcmpi(); ????

    or can i do like the word is Chris Sim
    then i enter Chris only and it will return mi the result
    Last edited by monkeymon; 04-08-2002 at 08:11 PM.

  8. #8
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    From MSDN...

    Code:
    #include <string.h>
    #include <stdio.h>
    
    char string1[] = "The quick brown dog jumps over the lazy fox";
    char string2[] = "The QUICK brown dog jumps over the lazy fox";
    
    void main( void )
    {
       char tmp[20];
       int result;
       /* Case sensitive */
       printf( "Compare strings:\n\t%s\n\t%s\n\n", string1, string2 );
       result = strcmp( string1, string2 );
       if( result > 0 )
          strcpy( tmp, "greater than" );
       else if( result < 0 )
          strcpy( tmp, "less than" );
       else
          strcpy( tmp, "equal to" );
       printf( "\tstrcmp:   String 1 is %s string 2\n", tmp );
       /* Case insensitive (could use equivalent _stricmp) */
       result = _stricmp( string1, string2 );
       if( result > 0 )
          strcpy( tmp, "greater than" );
       else if( result < 0 )
          strcpy( tmp, "less than" );
       else
          strcpy( tmp, "equal to" );
       printf( "\t_stricmp:  String 1 is %s string 2\n", tmp );
    }
    
    
    Output
    
    Compare strings:
       The quick brown dog jumps over the lazy fox
       The QUICK brown dog jumps over the lazy fox
    
       strcmp:   String 1 is greater than string 2
       _stricmp:  String 1 is equal to string 2
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  9. #9
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408
    Adrian what are you doing!?!?!?


    void main( void )

    Watch out or you'll get an angry Prelude in the head

  10. #10
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Originally posted by ErionD
    Adrian what are you doing!?!?!?





    Watch out or you'll get an angry Prelude in the head
    The important thing was his quote "From MSDN..."

    Some of the code on MSDN is ancient.......

  11. #11
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    >>> Watch out or you'll get an angry Prelude in the head.

    LOL. Fordy explained. I'll refer her to Bill!
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fucntion returns -1, Why?
    By Taper in forum C Programming
    Replies: 16
    Last Post: 12-08-2008, 06:30 PM
  2. help with switch statement
    By agentsmith in forum C Programming
    Replies: 11
    Last Post: 08-26-2008, 04:02 PM
  3. problem with strings
    By agentsmith in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 12:07 PM
  4. help with strcmp
    By blork_98 in forum C Programming
    Replies: 8
    Last Post: 02-21-2006, 08:23 PM
  5. strcmp
    By kryonik in forum C Programming
    Replies: 9
    Last Post: 10-11-2005, 11:04 AM