0

I'm trying to have a user enter a series of strings. This is my code.

this is my error message - cannot convert 'String' to 'char*' in assignment

char* titles[] = {"entry one", "entry two", "entry three", "entry four", "entry five"};


void setup() {
 Serial.begin(9600);
 Serial.println("Storing numbers in an array");
}

void loop()

{

for (int x =0; x < 5; x++)
{
  if (Serial.available() > 0)

  {
   Serial.println("Enter a word... ");
   titles[x] = Serial.readString();

    Serial.println(" ");
    Serial.println(chartitles[x]);

   }
  } 
 }
2
  • 1
    This is really not how you want to do this. May 5 '16 at 5:19
  • You are confusing the concept of a "string" (C string - char array) with a "String" - the Arduino-specific class that people who don't know better use.
    – Majenko
    May 5 '16 at 10:44
1

So, you are confusing the object String of Arduino with a string of C (char array), to solve your problem, I've changed your code in two parts:

String titles[] = {"entry one", "entry two", "entry three", "entry four", "entry five"};

void setup(){
  Serial.begin(9600);
  Serial.println("Storing numbers in an array");
}

void loop(){
  for (int x = 0; x < 5; x++){
    if (Serial.available() > 0){
      Serial.println("Enter a word... ");
      titles[x] = Serial.readString();
      Serial.println(" ");
      Serial.println(titles[x]);
    }
  }
}

So, in order to use Seria.readString(), you have to use String objects to receive the data from Serial port:

String titles[]

Instead of:

char* titles[]

The second change is related to Serial.println(chartitles[x]), I didn't understand what you mean with this code, so I changed in order to print the String that was read.

5
  • I modified the code slightly. Im getting a strange output. I placed the print statement outside the for loop to enter the strings. Now when it prints it prints the original values for the strings and the ones that are entered from the serial monitor. See the image. dropbox.com/sc/5ubcn2raee4vmkn/AADBvjHcQVMhpCsIIW3cahXba May 7 '16 at 4:55
  • @MichaelNiebauer I think this is what you want to code: codeshare.io/9ko5o May 10 '16 at 2:04
  • thank you, this does exactly what I was looking for. I have a question whats the difference between using if and while before the (Serial.available() > 0) statement. I was using an if and it didn't work it looks like by switching it to a while it does. May 10 '16 at 3:08
  • try to think about the else, if you were not writing something in the Serial console, the program would skip the if and continue, but since the code was inside a void loop, this would be repeated, making your program repeat the println and the for May 10 '16 at 18:02
  • when you use a while the program waits a response May 10 '16 at 18:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.