Char and string problem

Hello,
I want in a tft screen have some input from a a touchscreen to have 3 inputs (0 to 9). This code is working, but i get some strange results…
I make another test sketch to test this. Here i have the same results…
Can anyone tell me what I’m doing wrong
Thanks

String cText = "xxx";

void setup() {
Serial.begin(9600); 
Serial.println(cText);
delay(2000);
}

void loop(){

  cText = "___";
  Serial.println(cText);
  while (int i = String(cText).indexOf('_') != -1){
    // wait for some input, but this is working, now I changed it in i
     i= i+1;
     if(i = 1){
       cText[0] = "1";
       Serial.println (cText);
     }
     if(i = 2){
      cText[1] = "2";
      Serial.println (cText);
     }
     if(i = 3){
      cText[2] = "3";
      Serial.println (cText);
     }
     
  }

  delay(1000);
//  teller();
}

void teller(){
  String nameString = "Jacques Verleijen";  
  for (int counter = 0; counter <= nameString.length() - 1; counter++){  
     Serial.println(nameString[counter]);  
  }
}

if(i = 1){ when comparing use '=='if(i == 1){ that should make the result more predictable.

Don't confuse the assignment operator = (single equal sign) with the "equal to" comparison operator == (double equal signs)

Found the problem myself. Using single quotes in stead of double quotes solved the problem. I'm new in C programming (used to use Delphi). Can anyone tell me de diferrence between
String a= "1" and String a= '1' ?
Thanks

Have you fixed the problem pointed out in reply #1 ?

. Can anyone tell me de diferrence between String a= "1" and String a= '1' ?

Well whatever is in between double quotes is a String or char*/ char-array, what is between single quotes is a 'char'. The difference between the 2 statements you posted seems irrelevant, because i'd say that the 'char' gets cast to a String, and then assigned.

void teller(){
  String nameString = "Jacques Verleijen"; 
  for (int counter = 0; counter <= nameString.length() - 1; counter++){ 
     Serial.println(nameString[counter]); 
  }
}

Why not just?

void teller(){
  String nameString = "Jacques Verleijen"; 
  Serial.println(nameString); 
}

Or just?

  Serial.println("Jacques Verleijen");

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