expected unqualified-id before 'if' error??

Hello forum, im working on building a datalogger for my arduino volt meter. Im starting off with some "simple" coding to make sure I understand the code before i dig in. and I keep on getting an error and cannot pinpoint its location!! Here's the code:

#include <SD.h>
#include <SPI.h>
//SPI settings
// MOSI, MISO, SCLK Set by default

int CS_pin = 4;
int pow_pin = 10;

float refresh_rate = 0.0;

void setup()
{
Serial.begin(9600);
Serial.println("Initializing Card");
//CS Pin is an output
pinMode(CS_pin, OUTPUT);

//Card will draw power from pin 8, so set it high
pinMode(pow_pin, OUTPUT);
digitalWrite(pow_pin, HIGH);

if(!SD.begin(CS_pin)) //begin sd card process
{
Serial.println("Card Failed");
return; //ends program (terminate)
}
Serial.println("Card Ready");
}
//Read the Configuration info. (COMMANDS.txt)
File commandFile = SD.open("COMMANDS.txt");
if(commandFile)
{
Serial.println("Reading Command File");

float decade = pow(10, (commandFile.available() - 1));
while(commandFile.available())
{
float Volt = (commandFile.read() - '0');
refresh_rate = Volt*decade+refresh_rate;
decade = decade/10;
}
Serial.print("Refresh Rate = ");
Serial.print(refresh_rate);
Serial.println("ms");
}
else
{
Serial.println("Could not read command file.");
return;
}

}

void loop()
{
String dataString = "Elliott";
//Open a file to write to
//Only one file can be open at a time
File dataFile = SD.open("log.txt", FILE_WRITE);
if (dataFile)
{
dataFile.println(dataString);
dataFile.close();
Serial.println(dataString);
}
else
{
Serial.println("Couldnt access file");
}

delay(refresh_rate);
}

If anyone sees the issue please let me know! as this is very urgent.

Delta_G:
Check your braces. You have a bunch of code between setup and loop that's not in a function.

Sorry for my lack on knowlege, but by braces you mean "{}" these guys? and could you pinpoint that out for me?

Delta_G:
Yes. Those. In the IDE put you cursor right after the one at the beginning of setup and it will highlight the closing one that matches it. Is that where the setup function should end?

According to the tutorial I watched, yes. I tried moving the braces and it didnt change much, besides the fact that I had more errors. So i changed it back to its original formatting.

If you copy and pasted that code from the tutorial, then I wouldn't trust that tutorial. You actually have one too many braces.

{
   Serial.begin(9600);
   Serial.println("Initializing Card");
  //CS Pin is an output
   pinMode(CS_pin, OUTPUT);
 
   //Card will draw power from pin 8, so set it high
  pinMode(pow_pin, OUTPUT);
  digitalWrite(pow_pin, HIGH);
 

 
  if(!SD.begin(CS_pin)) //begin sd card process
  {
    Serial.println("Card Failed");
    return; //ends program (terminate)
  }
  Serial.println("Card Ready");
}<---- REMOVE
  //Read the Configuration info. (COMMANDS.txt)
  File commandFile = SD.open("COMMANDS.txt");
  if(commandFile)
  {

When you remove it, it will compile. When someone who has obviously more experience tells you a solution, you might want to dig deeper or ask more questions when you can't find it. Most of the members here that are trying to help won't just give you the answer, they will try to teach you how to find a solution yourself. And please use code tags.

And this is what Delta was talking about highlighting the braces to find the opening or closing brace.

codlink:
If you copy and pasted that code from the tutorial, then I wouldn't trust that tutorial. You actually have one too many braces.

{

Serial.begin(9600);
  Serial.println("Initializing Card");
  //CS Pin is an output
  pinMode(CS_pin, OUTPUT);

//Card will draw power from pin 8, so set it high
  pinMode(pow_pin, OUTPUT);
  digitalWrite(pow_pin, HIGH);

if(!SD.begin(CS_pin)) //begin sd card process
  {
    Serial.println("Card Failed");
    return; //ends program (terminate)
  }
  Serial.println("Card Ready");
}<---- REMOVE
  //Read the Configuration info. (COMMANDS.txt)
  File commandFile = SD.open("COMMANDS.txt");
  if(commandFile)
  {




When you remove it, it will compile. When someone who has obviously more experience tells you a solution, you might want to *dig* deeper or ask more questions when you can't find it. Most of the members here that are trying to help won't just give you the answer, they will try to teach you how to find a solution yourself.

And this is what Delta was talking about highlighting the braces to find the opening or closing brace.
![|403x500](http://forum.arduino.cc/index.php?action=dlattach;topic=377003.0;attach=153692)

Thank you!!
And that what i love about this forum! but the due date for this project is tomorrow, thats why its soo urgent!

eleinauer:
Sorry for my lack on knowlege, but by braces you mean "{}" these guys? and could you pinpoint that out for me?

Braces: {}
Brackets: [] (aka "square brackets")
Parenthesis: ()
Angle brackets: <>

Okay so im just about finished with the coding finally! but I have one final problem. Im getting this error:

Arduino_SD_card_Verify.ino:127:4: error: 'id' does not name a type
Arduino_SD_card_Verify.ino:129:9: error: expected constructor, destructor, or type conversion before '(' token
Arduino_SD_card_Verify.ino: In function 'void loop()':
Arduino_SD_card_Verify.ino:132:6: error: redefinition of 'void loop()'
Arduino_SD_card_Verify.ino:105:8: error: 'void loop()' previously defined here
Error compiling.

I removed the void loop() found directly above
"String dataString =String(id) + ", " + String(V_pin);"
But when I tried to begin my data-logging nothing would show up due to this missing code. Im not sure how to fix this error, any guidance would be greatly appreciated!!

Here is the code;

#include <LCD.h>
#include <LiquidCrystal.h>
#include <LiquidCrystal_I2C.h>
#include <SD.h>
#include <SPI.h> 
// I2C library

#include <Wire.h> 
//SPI settings
// MOSI, MISO, SCLK Set by default

int CS_pin = 4; 
int pow_pin = 10;
//analog pins for Voltage readings
int V_pin = 0;
#define I2C_ADDR    0x27 // Define I2C Address where the SainSmart LCD is
#define BACKLIGHT_PIN    6
#define En_pin  2
#define Rw_pin  1
#define Rs_pin  0
#define D4_pin  4
#define D5_pin  5
#define D6_pin  6
#define D7_pin  7

LiquidCrystal_I2C	lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
// initialize the library with the numbers of the interface pins
int sensePin = 0;
int timer;
long id = 1;
 //check voltage level
int Volt_level =analogRead(V_pin);


float refresh_rate = 0.0;

void setup()
 {
  analogReference(DEFAULT);
  // set up the LCD's number of columns and rows:
  lcd.begin(20, 4);
  timer=0;
 

   Serial.begin(9600);
   Serial.println("Initializing Card");
  //CS Pin is an output
   pinMode(CS_pin, OUTPUT);
  
   //Card will draw power from pin 8, so set it high
  pinMode(pow_pin, OUTPUT);
  digitalWrite(pow_pin, HIGH);
 

  
  if(!SD.begin(CS_pin)) //begin sd card process 
  {
    Serial.println("Card Failed");
    return; //ends program (terminate) 
  
  Serial.println("Card Ready");
}
  //Read the Configuration info. (COMMANDS.txt)
  File commandFile = SD.open("COMMANDS.txt");
  if(commandFile)
  {
     Serial.println("Reading Command File");
    
     float decade = pow(10, (commandFile.available() - 1));
     while(commandFile.available())
     {
       float Volt = (commandFile.read() - '0'); 
       refresh_rate = Volt*decade+refresh_rate;
       decade = decade/10; 
   }
     
     Serial.print("Refresh Rate = ");
     Serial.print(refresh_rate);
     Serial.println("ms");
     
   }
   else 
   {
     Serial.println("Could not read command file.");
     return;
   } 
   
     //Write Log File Header
     File logFile = SD.open("LOG.csv", FILE_WRITE);
     if (logFile)
     {
       logFile.println(", , , ,"); // Just a leading blank line, incase there was previous data 
       String header = "ID, Voltage";
       logFile.println(header);
       logFile.close();
       Serial.println(header);
     }
         else
    {
     Serial.println("Couldnt access file"); 
    }
   
 } 
  void loop ()
  {
  //Create Data string for storing to SD card
  //Utilizing CSV Format for Execl spreadsheet
  String dataString =String(id) + ", " + String(V_pin);
 
  //Open a file to write to
  //Only one file can be open at a time
  File logFile = SD.open("LOG.csv", FILE_WRITE);
  if (logFile)
   {
      logFile.println(dataString);
      logFile.close();
      Serial.println(dataString);
   } 
   else
   {
     Serial.println("Couldnt open log file"); 
   }
   
  }
   //Increment ID number
   id++;
   
   delay(refresh_rate);


void loop()
{
   delay(refresh_rate);

  // Print message to the LCD
  lcd.setCursor(0,0);
  lcd.print("            	");
  lcd.setCursor(0,0);
  lcd.print("Voltage Reading:");
 
  lcd.setCursor(0,1);
  lcd.print("            	");
  lcd.setCursor(0,1);
  lcd.print((analogRead(sensePin))/238.0);
  lcd.print("V");
  delay(1000);
 
  lcd.setCursor(0,2);
  lcd.print("Time Elapsed:");
 
  lcd.setCursor(0,3);
  lcd.print(timer);
  lcd.print(" Seconds");
  timer=timer+1;
}