Pages: [1]   Go Down
Author Topic: String comparison not working  (Read 844 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am trying to compare a string to a series of letters.  My code uploads and it will store the a and b I type in, but will not print equal when I type in three a. I am new to using strings so it could be an obvious problem. Any help would be very appreciated.

String charStorage;
int inByte = 0;
void setup(){
  Serial.begin(9600);
}
void loop(){
  if (Serial.available() > 0) {
   int inByte = Serial.read();
   switch(inByte){
     case 'a' :
     charStorage += 'a';
     Serial.println(charStorage);
     break;
     case 'b' :
     charStorage += 'b';
     Serial.println(charStorage);
     break;
 if(charStorage == "aaa" ){
 Serial.println("equal");
 }
  }
}
}
Logged

Offline Offline
Edison Member
*
Karma: 50
Posts: 1702
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Use Tools|Auto Format in the IDE to straighten out your code. You will then perhaps see that the if statement isn't in the right place.

Pete
Logged

Offline Offline
Sr. Member
****
Karma: 1
Posts: 462
I am a amateur.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Take the if statement out of the switch statement.
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you. That fixed it. This is helpful.
Logged

Fort Lauderdale, FL
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6144
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You really should be using strcmp() to compare strings, not ==.
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.c

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 310
Posts: 26636
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
You really should be using strcmp() to compare strings, not ==.
But s/he is using Strings, not strings, so == is fine.

(Does the String class even have a "strcmp" method?)
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 654
Posts: 50933
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Does the String class even have a "strcmp" method?
No, but OP should learn how to NOT use the String class. It's a damned resource hog, and provides little value in such a trivial program.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 310
Posts: 26636
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I can't disagree, but if the question relates to the String class, I feel we should respond in like.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Pages: [1]   Go Up
Jump to: