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.
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".
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.
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".
== 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.
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?