!= not working

Hey,

I've been having this problem for a while. When I enter 'f' in the serial monitor I get this back:

OK
Nope

If I immediately enter something other than 'f', it works fine

But I want that if you fill in 'f' you get 'Okay' and if you fill in something else you get 'Nope'. Why doesn't this work?

String inputString = "";
String focus = "";
char c;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  while (Serial.available())
  {
    c = (char)Serial.read();
    inputString += c;
  }

  //FOCUS
  if (inputString.length() > 0)
  {
    if(inputString == "f")
    {
      Serial.println("Ok");
      Serial.flush();
    }
    if(inputString != "f")
      {
        Serial.println("Nope");
        inputString = "";
      }
  }
}

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. This can happen after the program has been running perfectly for some time. Just use cstrings - char arrays terminated with '\0' (NULL).

When using Cstrings you must use strcmp() to compare values rather than ==

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

...R

Serial.flush() does not clear the input buffer, so most likely you are correctly seeing that the input is "f" and printing "OK", then you are adding the line ending character to inputString (probably set to newline), so that inputString is now != "f" and correctly prints "Nope".

Can I ask which you think more likely?

That the '!=' operator doesn't work, or that the things you are comparing really are different?

Robin2:
It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. This can happen after the program has been running perfectly for some time. Just use cstrings - char arrays terminated with ‘\0’ (NULL).

When using Cstrings you must use strcmp() to compare values rather than ==

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

…R

what am I doing wrong>?..

#include <string.h>

char c;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  while (Serial.available())
  {
    c = (char)Serial.read();
    //inputString += c;

    if((strcmp(c, "f") == 0))
    {
      Serial.println("Ok");
    }
    if((strcmp(c, "f") != 0))
    {
      Serial.println("Nope");
    }
  }
}

Is the serial monitor configured with “no line ending” ?

See reply #2

david_2018:
Serial.flush() does not clear the input buffer, so most likely you are correctly seeing that the input is “f” and printing “OK”, then you are adding the line ending character to inputString (probably set to newline), so that inputString is now != “f” and correctly prints “Nope”.

what should i change?

There is a question for you in reply #5

bidouilleelec:
Is the serial monitor configured with "no line ending" ?

No it stands on Newline

...and there is your answer.

craftercis:
No it stands on Newline

You got it!

bidouilleelec:
You got it!

ughh its monday guys ;D Thank you all!

craftercis:
what am I doing wrong>?...

Function strcmp expects pointers to C-strings. c is not a pointer to C-string.

== and != with strings is pointer-equality, not string equality. If both sides point to the same exact memory location they will compare equals with ==, otherwise they won't.

MarkT:
== and != with strings is pointer-equality, not string equality. If both sides point to the same exact memory location they will compare equals with ==, otherwise they won't.

What is this remark doing here? Where do you see == and != being used for "pointer equality" in this thread?

In the OP's code these operators are applied to String objects. And no, with String object == and != are NOT pointer equality. These operators do compare strings, fully and correctly.

Montmorency:
What is this remark doing here? Where do you see == and != being used for “pointer equality” in this thread?

In the OP’s code these operators are applied to String objects. And no, with String object == and != are NOT pointer equality. These operators do compare strings, fully and correctly.

See code in reply #4 :wink:

sterretje:
See code in reply #4 :wink:

Done. And???

Code in reply #4 does not use == or != for “pointer equality”. Code in reply #4 actually attempts to use strcmp, which is exactly the right thing to use (instead of != and == operators) when comparing C-strings.

The code uses strcmp incorrectly (as I noted in #13), but it still has nothing to do with the remark made by MarkT.

Hence the question, again: what is the relevance of that remark? Where did you see != and == being used to compare pointers in this topic?

You're right, my mistake there.