strcmp problem

Discussion in 'C++' started by Shane Peck, Sep 19, 2003.

  1. Shane Peck

    Shane Peck Guest

    I am having a problem using the strcmp function. If I debug my program from
    within Visual C++, strcmp returns 0 as I want, but if I build the project
    and then try and execute it, it seems to think the strings are different.
    I've got no idea why!! Any help appreciated, heres my code :

    AfxMessageBox( param, MB_OK, 0 );
    char *wow = "!";
    if( stricmp( param, wow ) == 0 )
    {//the parameter is !, so the line is a comment, skip over it
    AfxMessageBox( "Got a comment", MB_OK, 0 );

    The message box that I put before the comparison outputs an !, so I really
    have no idea why it's returning non-zero. I've also tried memcmp, and it
    gives the same result.

    Thanks for any help...
    Shane Peck, Sep 19, 2003
    #1
    1. Advertising

  2. Shane Peck

    White Wolf Guest

    Shane Peck wrote:
    > I am having a problem using the strcmp function. If I debug my
    > program from within Visual C++, strcmp returns 0 as I want, but if I
    > build the project and then try and execute it, it seems to think the
    > strings are different. I've got no idea why!! Any help appreciated,
    > heres my code :
    >
    > AfxMessageBox( param, MB_OK, 0 );
    > char *wow = "!";
    > if( stricmp( param, wow ) == 0 )
    > {//the parameter is !, so the line is a comment, skip over it
    > AfxMessageBox( "Got a comment", MB_OK, 0 );
    >
    > The message box that I put before the comparison outputs an !, so I
    > really have no idea why it's returning non-zero. I've also tried
    > memcmp, and it gives the same result.


    Print those two variables to the debug console as strings (note: not
    debugger, debug console) and make sure you print them with some delimiters
    around. If strcmp says they are not the same I strongly believe that they
    are not the same.

    --
    WW aka Attila
    White Wolf, Sep 19, 2003
    #2
    1. Advertising

  3. Shane Peck

    Chris Theis Guest

    "Shane Peck" <> wrote in message
    news:3f6a4fb0$0$23586$...
    > I am having a problem using the strcmp function. If I debug my program

    from
    > within Visual C++, strcmp returns 0 as I want, but if I build the project
    > and then try and execute it, it seems to think the strings are different.
    > I've got no idea why!! Any help appreciated, heres my code :
    >
    > AfxMessageBox( param, MB_OK, 0 );
    > char *wow = "!";
    > if( stricmp( param, wow ) == 0 )
    > {//the parameter is !, so the line is a comment, skip over it
    > AfxMessageBox( "Got a comment", MB_OK, 0 );
    >
    > The message box that I put before the comparison outputs an !, so I really
    > have no idea why it's returning non-zero. I've also tried memcmp, and it
    > gives the same result.
    >
    > Thanks for any help...
    >


    If I get you correctly then any line containing (starting?) a with a "!"
    should be skipped. What you're looking for is not string comparison to
    another string but to search for a character in a string. The solution to
    your problem is either to use the string class which will make your life a
    lot easier or to use strchr:

    char pParam1[] = "! this is a comment";
    char pParam2[] = "this is NO comment!";
    int Wow = '!';
    char* pRet;

    pRet = strchr( pParam1, Wow );
    if( pRet && static_cast<int>(pRet - pParam1 + 1) == 1 ) // probably you
    might encounter blanks before the comment symbol which have to be stripped
    before this is check is done!!!
    cout << "Comment found: " << pParam1 << endl;
    else
    cout << "No comment found: " << pParam2 << endl;

    pRet = strchr( pParam2, Wow );
    if( pRet && static_cast<int>(pRet - pParam2 + 1) == 1 )
    cout << "Comment found: " << pParam2 << endl;
    else
    cout << "No comment found: " << pParam2 << endl;

    HTH
    Chris
    Chris Theis, Sep 19, 2003
    #3
  4. "Shane Peck" <> wrote in message
    news:3f6a4fb0$0$23586$...
    > I am having a problem using the strcmp function. If I debug my program

    from
    > within Visual C++, strcmp returns 0 as I want, but if I build the project
    > and then try and execute it, it seems to think the strings are different.
    > I've got no idea why!! Any help appreciated, heres my code :
    >
    > AfxMessageBox( param, MB_OK, 0 );
    > char *wow = "!";
    > if( stricmp( param, wow ) == 0 )


    just a guess: maybe the problem is that you are using 'stricmp' instead of
    'strcmp' ?

    > {//the parameter is !, so the line is a comment, skip over it
    > AfxMessageBox( "Got a comment", MB_OK, 0 );
    Klaus Eichner, Sep 19, 2003
    #4
  5. "Shane Peck" <> writes:

    > I am having a problem using the strcmp function. If I debug my program from


    here you say you are using strcmp which is a standard C function - ok.

    > within Visual C++, strcmp returns 0 as I want, but if I build the project
    > and then try and execute it, it seems to think the strings are different.
    > I've got no idea why!! Any help appreciated, heres my code :
    >
    > AfxMessageBox( param, MB_OK, 0 );
    > char *wow = "!";
    > if( stricmp( param, wow ) == 0 )


    but here you use stricmp (note the 'i')
    (which isn't standard and hence OT in c.l.c++, BTW).

    Which one are you using actually? And post some minimal compilable code
    illustrating the problem.

    HTH & kind regards
    frank

    --
    Frank Schmitt
    4SC AG phone: +49 89 700763-0
    e-mail: frankNO DOT SPAMschmitt AT 4sc DOT com
    Frank Schmitt, Sep 19, 2003
    #5
  6. "Frank Schmitt" <> wrote in message
    news:4c4qz9t1qv.fsf@scxw21.4sc...
    > "Shane Peck" <> writes:
    > >
    > > AfxMessageBox( param, MB_OK, 0 );
    > > char *wow = "!";
    > > if( stricmp( param, wow ) == 0 )

    >
    > but here you use stricmp (note the 'i')


    Using stricmp should have no effect in the example
    code, though (note the '!' which is the same in upper
    and lower case).

    - Risto -
    Risto Lankinen, Sep 22, 2003
    #6
  7. Risto Lankinen wrote:

    > "Frank Schmitt" <> wrote in message
    > news:4c4qz9t1qv.fsf@scxw21.4sc...
    >
    >>"Shane Peck" <> writes:
    >>
    >>> AfxMessageBox( param, MB_OK, 0 );
    >>> char *wow = "!";
    >>> if( stricmp( param, wow ) == 0 )

    >>
    >>but here you use stricmp (note the 'i')

    >
    >
    > Using stricmp should have no effect in the example
    > code, though (note the '!' which is the same in upper
    > and lower case).
    >


    We can't know how stricmp() will affect the example. The definition of
    stricmp() was not given, and it's not a standard function, so we have no
    idea what it does.

    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
    Kevin Goodsell, Sep 22, 2003
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. muser

    strcmp

    muser, Oct 3, 2003, in forum: C++
    Replies:
    6
    Views:
    1,112
    Frank Schmitt
    Oct 9, 2003
  2. Andrej Hocevar

    please help with strcmp()

    Andrej Hocevar, Jul 19, 2003, in forum: C Programming
    Replies:
    3
    Views:
    325
    Gordon Burditt
    Jul 19, 2003
  3. collinm

    strtok problem - strcmp

    collinm, Mar 24, 2005, in forum: C Programming
    Replies:
    4
    Views:
    792
    Mark McIntyre
    Mar 24, 2005
  4. rabbits77

    weird problem with strcmp()

    rabbits77, Apr 1, 2010, in forum: C Programming
    Replies:
    20
    Views:
    872
  5. Chad

    Re: Strange strcmp() problem

    Chad, May 15, 2010, in forum: C Programming
    Replies:
    8
    Views:
    354
    Tim Rentsch
    Jun 18, 2010
Loading...

Share This Page