Page 1 of 2

why is this strcmp not working?

Posted: 21 Feb 2012, 21:11
by brothman01
yes, I saw in another thread that somone said strcmp did not work the way one would expect, and to read the documentation... but I did that, and it still does not work.

Code: Select all

   int cmdComp = strcmp(input, "foobar");
   if (cmdComp == 0) {
     PlayTone(TONE_A4, MS_500);
     Wait(1000);
   } else {
    PlayTone(TONE_A4, MS_500);
    Wait(1000);
   }
Assume there is a non-foobar variable assigned to input. So the if-statement should execute the "else" part...

anyway, with this code, it does not even compile, what did I do wrong?

Re: why is this strcmp not working?

Posted: 21 Feb 2012, 21:45
by HaWe
hi,
consider that it might be always a good idea to post the whole code which is supposed to work or not ;)

Re: why is this strcmp not working?

Posted: 21 Feb 2012, 23:49
by afanofosc
It could be an older compiler version causing your troubles. Are you using the latest test release?

Code: Select all

task main()
{
  string input = "fooba";
   int cmdComp = strcmp(input, "foobar");
   NumOut(0, LCD_LINE2, cmdComp);
   if (cmdComp == 0) {
     TextOut(0, LCD_LINE1, "cmdComp==0");
     Wait(1000);
   } else {
     TextOut(0, LCD_LINE1, "cmdComp!=0");
    Wait(1000);
   }
   while(true);
}
This compiles and runs fine for me. It says cmdComp!=0 and -1 (since "fooba" is less than "foobar"). If I change input to "testing" then I get cmdComp!=0 and 1. If I change input to "foobar" then I get cmdComp==0 and 0.

John Hansen

Re: why is this strcmp not working?

Posted: 22 Feb 2012, 08:02
by HaWe
now it's also clear to me, thx, I was courious about the "input".
I knew it just from C as str1 and str2
Syntax:

Code: Select all

#include <string.h>
int strcmp( const char *str1, const char *str2 );
Description:
The function strcmp() compares str1 and str2, then returns:

Code: Select all

Return value     Explanation
------------     -----------
less than 0 	  str1 is "lexically" less than str2
equal to 0       str1 is "lexically" equal to str2
greater than 0   str1 is "lexically" greater than str2

Re: why is this strcmp not working?

Posted: 22 Feb 2012, 18:13
by brothman01
hi,
consider that it might be always a good idea to post the whole code which is supposed to work or not ;)
thanks for the tip, I thought only posting the pertinent part was necessary, but I will post the whole thing from now on.. sry

Wow, things are really deteriorating around here, lol... I can't even compile or compile/upload code now... It gets Error 1 from trying compile the following code:

Code: Select all

task main()
{
  int num = 5;
}
what problem is causing this error?
It could be an older compiler version causing your troubles. Are you using the latest test release?
I think so?

nbc -version
Next Byte Codes Compiler version 1.0.1.b35 (1.0.1.35, built Sun Oct 19 16:50:26 CDT 2008)
Copyright (c) 2006, John Hansen


now it's also clear to me, thx, I was courious about the "input".

I knew it just from C as str1 and str2

Syntax:

Code: Select all

#include <string.h>
int strcmp( const char *str1, const char *str2 );
while I am aware that that is the C code, does NXC require those character array pointers, or can we use the C++ class of 'string' ?

Re: why is this strcmp not working?

Posted: 22 Feb 2012, 18:25
by HaWe
of course this is ANSI C.
As we lack of pointers, instead of ANSI C
char *str1
you should use NXC
string str1

and I suppose your BCC version is not at the very state of art ;)

Re: why is this strcmp not working?

Posted: 22 Feb 2012, 21:18
by afanofosc
Which platform are you running the NBC command line compiler on, i.e., windows, mac, or linux?

If you are using Windows then I highly recommend using the latest BricxCC IDE test release and its internal NBC compiler. If you can't do that then definitely grab the latest test build of the command line compiler from the latest BricxCC test release zip at this link: http://bricxcc.sourceforge.net/test_releases/

If you are using Mac OS X or Linux then the latest build available for you to use is here:

http://bricxcc.sourceforge.net/nbc/release/index.html

It is build 1.2.1 r5. It looks like you are getting your compiler from the beta releases page which is not the right place to look. My apologies for any confusion caused by my website layout.

John Hansen

Re: why is this strcmp not working?

Posted: 22 Feb 2012, 22:40
by brothman01
Which platform are you running the NBC command line compiler on, i.e., windows, mac, or linux?
Mac OS X 10.6
It is build 1.2.1 r5. It looks like you are getting your compiler from the beta releases page which is not the right place to look.
My bad.. I will get the newer version of the compiler.. thank you! Oh, the unzipped download produces a folder with a .rfw (brick firmware?), a .exe file, release notes, and a few other things.. this seems very different from the other, older version.. is there a tutorial for setting up this version?
It is build 1.2.1 r5. It looks like you are getting your compiler from the beta releases page which is not the right place to look. My apologies for any confusion caused by my website layout.
No problem, in fact thank you... you actually made and maintain a website for it..
of course this is ANSI C.
As we lack of pointers, instead of ANSI C
char *str1
you should use NXC
string str1

and I suppose your BCC version is not at the very state of art ;)
Judging from your use of ';)' I feel like you are being sarcastic, but if you are not then I'm not sure why your intention is to offend me. And from your rather cryptic and nonsensical answer, I have taken the following partial meaning:

of course this is ANSI C. (As you may or may not have known, this language is based on and derived from ANSI C.)
As we lack of pointers, instead of ANSI C you should use NXC (NXC does not use pointers.)
you should use NXC: string str1
[How is that possible:
• ANSI C uses pointers
• NXC is part of ANSI C
therefore NXC uses pointers]

so is that a yes on using the string class or not?

Re: why is this strcmp not working?

Posted: 22 Feb 2012, 22:46
by HaWe
I think you misunderstand my postings, maybe google translate did not clearly translate what I wanted to say actually.
;) was only intended to give you a hint, I doubt that this is meant "sarcastic" ever, not even ironic
(ironic means: to say the opposite of that what is spoken, like: "what a beautiful weather!" while it actually pours - sarcastic similar, but additionally evil or twisted evil; of course none of that was my intention.)

I don't know what you mean by your C++ classes, I'm not programming in C++, I don't use classes ever, I just use standard C syntax (ANSI C99).
But NXC is not part of ANSI C (no subset), unfortunately, it is only a small intersection set with many non-standard extensions or alterations.

Re: why is this strcmp not working?

Posted: 22 Feb 2012, 23:38
by brothman01
oh man, I'm so sorry, I did not realize you were translating your answers from another language! :) I thought you were just being condescending to me.

oh, when I said 'C++ Classes' I meant C++ structures... like the struct thing I was thinking of java for a second ;) .
But NXC is not part of ANSI C (no subset)
thank you for explaining this part, I understand now, thank you! So does that mean that NXC does not have pointers and so I should say:
string foo
OR
const char *foo ???