Error: expected declaration before '{' token

I received an error with my code. Im trying to get a song to play alongside its name on an LCD screen. I was unable to do this all in one tab, so i split up the LCD, the actual sounds, and the pitches. The line that received the error is in bold; any help would be greatly appreciated!

int melody = {
NOTE_B3, NOTE_D4, NOTE_FS4, NOTE_B3, NOTE_D4, NOTE_FS4, NOTE_B3,
NOTE_D4, NOTE_CS4, NOTE_E4, NOTE_GS4, NOTE_CS4,
NOTE_E4, NOTE_GS4, NOTE_CS4, NOTE_E4, NOTE_D4,
NOTE_FS4, NOTE_A4, NOTE_D4, NOTE_FS4, NOTE_A4, NOTE_D4,
NOTE_FS4, NOTE_A4, NOTE_D4, NOTE_FS4, NOTE_A4,
NOTE_CS4, NOTE_E4, NOTE_GS4, NOTE_E4, NO_SOUND
};

int noteDurations = {
12, 12, 12, 12, 12, 12, 12, 12,
12, 12, 12, 12, 12, 12, 12, 12,
12, 12, 12, 12, 12, 12, 12, 12,
12, 12, 12, 12, 12, 12, 12, 12, 0
};

{ for (int thisNote = 0; thisNote < 100; thisNote++);
// to calculate the note duration, take one second
// divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 1800 / noteDurations[thisNote];
// I found that by lengthening the noteDuration,
// I was able to “slow” the song down.
tone(7, melody[thisNote], noteDuration);

// to distinguish the notes, set a minimum time between them.
// the note’s duration + 30% seems to work well:
delay(noteDuration);

int pauseBetweenNotes = noteDuration * 0.30;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(7);
};

You seriously need to just look up the syntax of a for statement. The curly brace comes after it and there is no semicolon after.

Post all the code, post the error in full, is the advice given…

Anyway that snippet is obviously not correct C as you have a code block outside of
any function definition, which is not allowed.

If i put void setup or anything like that, an error shows up that says “already defined” and this is the full code:

#include “pitches.h”

// notes in the melody:
int melody = {
NOTE_B3, NOTE_D4, NOTE_FS4, NOTE_B3, NOTE_D4, NOTE_FS4, NOTE_B3,
NOTE_D4, NOTE_CS4, NOTE_E4, NOTE_GS4, NOTE_CS4,
NOTE_E4, NOTE_GS4, NOTE_CS4, NOTE_E4, NOTE_D4,
NOTE_FS4, NOTE_A4, NOTE_D4, NOTE_FS4, NOTE_A4, NOTE_D4,
NOTE_FS4, NOTE_A4, NOTE_D4, NOTE_FS4, NOTE_A4,
NOTE_CS4, NOTE_E4, NOTE_GS4, NOTE_E4, NO_SOUND,
};

int noteDurations = {
12, 12, 12, 12, 12, 12, 12, 12,
12, 12, 12, 12, 12, 12, 12, 12,
12, 12, 12, 12, 12, 12, 12, 12,
12, 12, 12, 12, 12, 12, 12, 12, 0,
};

for {(int thisNote = 0; thisNote < 100; thisNote++);
// to calculate the note duration, take one second
// divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 1800 / noteDurations[thisNote];
// I found that by lengthening the noteDuration,
// I was able to “slow” the song down.
tone(7, melody[thisNote], noteDuration);

// to distinguish the notes, set a minimum time between them.
// the note’s duration + 30% seems to work well:
delay(noteDuration);

int pauseBetweenNotes = noteDuration * 0.30;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(7);
};

This is the full error

Sounds.ino:20:1: error: expected unqualified-id before 'for' Error compiling.

Someone please help, ive been trying to get rid of this error for the past 3 hours.

This is the error right now

Capture.PNG

Everything from the for loop down needs to be inside of a function. That type of code is not allowed outside of a function.

You'll also get errors if you don't have the loop and setup functions defined.

how do i put multiple codes inside the void setup or loop without messing up the formatting?

SN9911:
This is the full error

Sounds.ino:20:1: error: expected unqualified-id before ‘for’
Error compiling.

Hey. Yo. I already explained that one to you.

for {(int thisNote = 0; thisNote < 100; thisNote++);

should be

for (int thisNote = 0; thisNote < 100; thisNote++) {

That still wouldn’t compile if the for loop isn’t inside a function.

OP, you need to take a look at some of the example sketches to see how code gets laid out. You can put as much code as you want, within the memory limits of the chip, into either the setup or loop functions. Or you can write another function with whatever name you want. But all the code has to be inside a function aside from global variable declarations and preprocessor stuff.

i have to have this project done by monday, im soo screwed!! and aarg, i did your way, the error was still there.

Well how about reposting what you have, in proper code tags? There's lots of time before Monday. But you need to read all the responses carefully. Not just mine.

#include "pitches.h"

// notes in the melody:
int melody[] = {
  NOTE_B3, NOTE_D4, NOTE_FS4, NOTE_B3, NOTE_D4, NOTE_FS4, NOTE_B3,
  NOTE_D4, NOTE_CS4, NOTE_E4, NOTE_GS4, NOTE_CS4,
  NOTE_E4, NOTE_GS4, NOTE_CS4, NOTE_E4, NOTE_D4,
  NOTE_FS4, NOTE_A4, NOTE_D4, NOTE_FS4, NOTE_A4, NOTE_D4,
  NOTE_FS4, NOTE_A4, NOTE_D4, NOTE_FS4, NOTE_A4,
  NOTE_CS4, NOTE_E4, NOTE_GS4, NOTE_E4, NO_SOUND,
};

int noteDurations[] = {
  12, 12, 12, 12, 12, 12, 12, 12,
  12, 12, 12, 12, 12, 12, 12, 12,
  12, 12, 12, 12, 12, 12, 12, 12,
  12, 12, 12, 12, 12, 12, 12, 12, 0,
};

{ for (int thisNote = 0; thisNote < 100; thisNote++){
    // to calculate the note duration, take one second
    // divided by the note type.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
    int noteDuration = 1800 / noteDurations[thisNote];
  // I found that by lengthening the noteDuration,
  // I was able to "slow" the song down.
  tone(7, melody[thisNote], noteDuration);

  // to distinguish the notes, set a minimum time between them.
  // the note's duration + 30% seems to work well:
  delay(noteDuration);

  int pauseBetweenNotes = noteDuration * 0.30;
  delay(pauseBetweenNotes);
  // stop the tone playing:
  noTone(7);}
}}

These are the errors i get with the above code

Headlines2.ino:4:6: error: expected unqualified-id before '{' token Sounds.ino:20:1: error: expected unqualified-id before '{' token Error compiling.

#include "pitches.h"

// notes in the melody:
int melody[] = {
  NOTE_B3, NOTE_D4, NOTE_FS4, NOTE_B3, NOTE_D4, NOTE_FS4, NOTE_B3,
  NOTE_D4, NOTE_CS4, NOTE_E4, NOTE_GS4, NOTE_CS4,
  NOTE_E4, NOTE_GS4, NOTE_CS4, NOTE_E4, NOTE_D4,
  NOTE_FS4, NOTE_A4, NOTE_D4, NOTE_FS4, NOTE_A4, NOTE_D4,
  NOTE_FS4, NOTE_A4, NOTE_D4, NOTE_FS4, NOTE_A4,
  NOTE_CS4, NOTE_E4, NOTE_GS4, NOTE_E4, NO_SOUND,
};

int noteDurations[] = {
  12, 12, 12, 12, 12, 12, 12, 12,
  12, 12, 12, 12, 12, 12, 12, 12,
  12, 12, 12, 12, 12, 12, 12, 12,
  12, 12, 12, 12, 12, 12, 12, 12, 0,
};


void playTheSong(){

 for (int thisNote = 0; thisNote < 100; thisNote++){
    // to calculate the note duration, take one second
    // divided by the note type.
    //e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
    int noteDuration = 1800 / noteDurations[thisNote];
  // I found that by lengthening the noteDuration,
  // I was able to "slow" the song down.
  tone(7, melody[thisNote], noteDuration);

  // to distinguish the notes, set a minimum time between them.
  // the note's duration + 30% seems to work well:
  delay(noteDuration);

  int pauseBetweenNotes = noteDuration * 0.30;
  delay(pauseBetweenNotes);
  // stop the tone playing:
  noTone(7);}
}
}

OK, since you obviously didn’t get the comments earlier, I’ve fixed it for you. I took the aberrant brace off your for loop (aarg’s comment) and moved the code into a function named playTheSong. Now what do you get?

Here’s my bet on the new errors:

undefined reference to ‘setup’
undefined reference to ‘loop’

You MUST have a function named setup and a function named loop defined in your code. MUST, it is NOT optional if you are compiling with the Arduino IDE. They can be empty if you want, but they MUST be defined.

However, if they are both empty be forewarned that your code won't do anything. It will compile, but it won't do squat since those are the only two functions called by main. (Aside from that silly SerialEventRun nonsense that we won't get into here)

If i put void setup or anything like that, an error shows up that says "already defined" and this is the full code:

Don't write random garbage and expect it to work.

And don't create multiple tabs for your code, when you obviously don't know what you are doing. If you don't know what the words "scroll bar" mean, I suggest you look it up.

This is obviously an attempted modification of the tone tutorial:

http://www.arduino.cc/en/Tutorial/Tone