strcmp problem

S

Shane Peck

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

White Wolf

Shane said:
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.
 
C

Chris Theis

Shane Peck said:
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
 
K

Klaus Eichner

Shane Peck said:
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' ?
 
F

Frank Schmitt

Shane Peck said:
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
 
R

Risto Lankinen

Frank Schmitt said:
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 -
 
K

Kevin Goodsell

Risto said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top