strcmp problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Shane Peck

    strcmp problem

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


  • White Wolf

    #2
    Re: strcmp problem

    Shane Peck wrote:[color=blue]
    > 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.[/color]

    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


    Comment

    • Klaus Eichner

      #3
      Re: strcmp problem

      "Shane Peck" <shanepeck@iine t.net.au> wrote in message
      news:3f6a4fb0$0 $23586$5a62ac22 @freenews.iinet .net.au...[color=blue]
      > I am having a problem using the strcmp function. If I debug my program[/color]
      from[color=blue]
      > 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 )[/color]

      just a guess: maybe the problem is that you are using 'stricmp' instead of
      'strcmp' ?
      [color=blue]
      > {//the parameter is !, so the line is a comment, skip over it
      > AfxMessageBox( "Got a comment", MB_OK, 0 );[/color]



      Comment

      • Frank Schmitt

        #4
        Re: strcmp problem

        "Shane Peck" <shanepeck@iine t.net.au> writes:
        [color=blue]
        > I am having a problem using the strcmp function. If I debug my program from[/color]

        here you say you are using strcmp which is a standard C function - ok.
        [color=blue]
        > 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 )[/color]

        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

        Comment

        • Chris Theis

          #5
          Re: strcmp problem


          "Shane Peck" <shanepeck@iine t.net.au> wrote in message
          news:3f6a4fb0$0 $23586$5a62ac22 @freenews.iinet .net.au...[color=blue]
          > I am having a problem using the strcmp function. If I debug my program[/color]
          from[color=blue]
          > 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...
          >[/color]

          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


          Comment

          • Risto Lankinen

            #6
            Re: strcmp problem


            "Frank Schmitt" <frank.schmitt@ 4sc.com> wrote in message
            news:4c4qz9t1qv .fsf@scxw21.4sc ...[color=blue]
            > "Shane Peck" <shanepeck@iine t.net.au> writes:[color=green]
            > >
            > > AfxMessageBox( param, MB_OK, 0 );
            > > char *wow = "!";
            > > if( stricmp( param, wow ) == 0 )[/color]
            >
            > but here you use stricmp (note the 'i')[/color]

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

            - Risto -



            Comment

            • Kevin Goodsell

              #7
              Re: strcmp problem

              Risto Lankinen wrote:
              [color=blue]
              > "Frank Schmitt" <frank.schmitt@ 4sc.com> wrote in message
              > news:4c4qz9t1qv .fsf@scxw21.4sc ...
              >[color=green]
              >>"Shane Peck" <shanepeck@iine t.net.au> writes:
              >>[color=darkred]
              >>> AfxMessageBox( param, MB_OK, 0 );
              >>> char *wow = "!";
              >>> if( stricmp( param, wow ) == 0 )[/color]
              >>
              >>but here you use stricmp (note the 'i')[/color]
              >
              >
              > Using stricmp should have no effect in the example
              > code, though (note the '!' which is the same in upper
              > and lower case).
              >[/color]

              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.

              Comment

              Working...