Verifying a string within an if statement

Hello everyone! I'm prompted with creating a program that reads a user entered string, sees if it matches a predefined string, and if it does then it will light a green light, and if it doesn't then it will light a red light. I haven't added the light related code yet since I'm trying to break this down piece by piece, but what I'm getting hasn't been successful. I'll enter the correct string (that being Rocket) and the program will recognize it as being invalid. Any and all feedback is very appreciated! Here is my code:

String enteredUser;
void setup() {
  Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}

void loop() {

  Serial.println("Please enter your username or ID: "); 

  while (Serial.available() == 0)
  {}
    enteredUser = Serial.readString();

  //if (enteredUser == "Rocket")
  if(Serial.readString() == "Rocket")
  { Serial.println (enteredUser);
    Serial.println("Hello Rocket Raccoon");
  }
  else 
  {
    Serial.println("Invalid Username");
    }
}

Hello pynkkfur
What this means?

Have a nice day and enjoy programming in C++ and learning.
Дайте миру шанс!

That is probably Rocket\r\n. Check the line ending setting in the serial monitor (assuming you're using that).

Note that string is not the same as String.

Reading Serial Input Basics - updated might give you some ideas for serial communication.

While you’re early in the journey, consider if caSe is important in your entry…
Rocket isn’t the same as rocket.

In my code, I convert all input to lowercase before I process it (unless caSE is important.

That also makes it easier that a user doesn’t have to be keyboard or case aware… they just roll up to the keyboard and type it any old way - the parser doesn’t care.

p.s. the CR / LF really has no reason to be saved as parts of your input. Detect them if needed for execution etc, then throw them away.

Likely you are not allowing for the line ending that the monitor puts on the end of your typed String. Either you need to allow for it in the String you compare to, or turn off in the monitor.

image

Thank you for your reply! I'll enter Rocket and instead of displaying "Hello Rocket Raccoon" it shows "Please enter your username or ID: "

Thank you for your reply! I had it set to Newline, then I switched it to No line ending, though I'm just getting the "Please enter your username or ID: " after I type in "Rocket".

Thank you for your reply! Yes, I remember learning that C++ and Arduino is case sensitive, so I've been sure to enter "Rocket". Also, what does CR / LF mean and how would I go about erasing them?

Thank you for your reply! I had it previously set to Newline but switched to No line ending, though nothing changed. I had taken a course in C++ where string was introduced as data containing multiple letters so I figured that it would be the same here. Am I using the wrong data type?

Try...

  if (enteredUser == "Rocket")
  //if(Serial.readString() == "Rocket")

You should be checking the String that you just read, not reading again.

1 Like

The Serial Monitor and the basic String functions are pretty clunky, and you have to leave a lot of the thinking to them.

Now, You’ve seen how the Carriage Return and Line Feed characters affect your input.
If you do eventually migrate to using c-strings, you’ll discover a bit more work, but a lot more flexibility with tighter code, that allows many tricks and better character handling.

While you’re still learning, get used to the basics, then get brave later!

How one can miss the obvious :frowning:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.