Hello everyone,
Im trying to compare a variable "slide" to a string "SRT"
I'm printing to my serial monitor the variable and getting what appears to be the identical string "SRT"
However when I try to compare them inside an if statement ( if(slide == "SRT")) it does not go through.
If I switch it to something where I only compare 1 letter for example if(slide[1] == 'R') that will work fine.
I've tried using strcmp, to compare the strings but I get the following error when compiling:
"cannot convert 'String' to const char* for argument 1 to int strcmp(const char*, const char *)"
I've refrained from posting my code because it is extremely convoluted and I think my misunderstanding is fundamental. Perhaps it is something to do with the variable slide being a pointer and not a string?
Thanks for any suggestions,
However when I try to compare them inside an if statement ( if(slide == "SRT")) it does not go through.
That won't work for 2 Strings, and it won't work for 2 character arrays either, and it certainly won't work for one of each.
To compare 2 character arrays, I suggest you google strcmp( ) function.
To compare 2 Strings, it is usually something like StringA.equals( StringB )
To compare one of each, you probably need to convert one or the other and then use one of the 2 methods I just mentioned.
You are correct, you are comparing pointers, not their data.
To use strcmp with a String object, you can return the internal buffer using the c_str() method.
[color=maroon]strcmp( slide.c_str(), "SRT" );[/color]
However seeing as you're using a String, you can just compare directly as the library accepts c-string types.
[color=maroon]if(slide == "SRT"){
//...
}[/color]
thanks so much pyro! got my code working now