Concatenation

Hi,

I am somewhat new to C\C++ syntax and need help with the following:

// trying to concatenate y to myVar    
for (int y = 0; y < 5; y++) {
   myFunction(myVar & y)
}

What is the easiest way to accomplish this?

Thanks!

Matt

Do you want "0 1 2 3 4" or maybe "01234"?

myVar = "somestring"
// trying to concatenate y to myVar    
for (int y = 0; y < 5; y++) {
   myFunction(myVar & y)
}

Output:
somestring0
somestring1
somestring2

Basically, it's just not that easy. All your variables need to have types, so you are probably looking at strings here. Have a look at the docs for the String object: http://arduino.cc/en/Reference/StringObject and particularly concat() http://arduino.cc/en/Reference/StringConcat

Will you be using this concatenated string at a later stage in your program or do you want to just make this output and move on to other stuff? If you need this string later, you are asking how to create this string and save it. If you just need to output it once you have different options.

no need to save it...just trying to clean this up:

play_rtttl(song1); delay(300); knockOut(0); delay(600); knockOut(0); delay(300); play_rtttl(song2); delay(300); knockOut(0); delay(600); knockOut(0); delay(300); play_rtttl(song3); delay(300); knockOut(0); delay(600); knockOut(0); delay(300);

what is the purpose of the delay()/knockOut() calls? Maybe you should write a function to put them in, since you have the same pattern 3 times.

Ok if you don’t need to store the string its easier. Do the following:

char msg =“string”;
for (i=0;i<5;i++)
{
Serial.print(msg);
Serial.println(i);
}

kidmosey:

A friend and I made a version of the Piecax poltergeist http://www.youtube.com/watch?v=DSk1qTC_2uQ

The delay() and knockOut() calls are the 'finger snapping' in the Addams Family theme http://www.youtube.com/watch?v=zA6fRAKuK8Y

It will also 'knock back' if you tap a pattern of knocks http://www.youtube.com/watch?v=4SxaMaGCadU

liudr:

Thanks for your reply but not sure how to implement that into my code?

Matt

Okay, are you concatenating onto strings or variable names?

It looks like you want to concatenate song[1,2,3], which point to the actual song titles. If you create an array of the strings, instead, you can put that into a for loop.

char *songs[3] = {
  "song1.wav",
  "song2.wav",
  "song3.wav"
};

for (int i = 0; i < 3; i++)
  myFunction(songs[i]);