a function definition not allowed before "{ "

I try to get data from 2 Temp.sensors to a data logger.
To do, i took a running sketch of the 2 sensors ond copied the data part into it and modified it in several parts.

I checked several times and experiented with several "{ "and "} " ,s but kept this error message coming up.
Could you help me pls.

As it shows, i am a newby , it surprices me that a datafile is "opend" all the time.
"File dataFile = SD.open("datalog.txt", FILE_WRITE);" within the loop and not i the setup part.
Is this usual ?

// Logger - Version: 1.0.3
#include <Logger.h>
#include <SimpleDHT.h>
#include <SPI.h>
#include <SD.h>

//      VCC: 5V or 3V
//      GND: GND
//      SENSOR 1 is in Digital Data pin: 7
//      SENSOR 2 is in Digital Data pin: 8

const int chipSelect = 4;
int dataPinSensor1 = 7;
int dataPinSensor2 = 8;

SimpleDHT22 dht22;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  
  Serial.print("Initializing SD card...");

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    while (1);
  
  }
  
Serial.println("card initialized.");

void loop() {
 
 File dataFile = SD.open("datalog.txt", FILE_WRITE);
 
// Reading data from sensor 1...
  Serial.println("=================================");

  // Reading data from sensor 1...
  Serial.println("Getting data from sensor 1...");
  
  // read without samples.
  byte temperature = 0;
  byte humidity = 0;
  int err = SimpleDHTErrSuccess;
  if ((err = dht22.read(dataPinSensor1, &temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
    
    Serial.print("Communication error with Sensor 1, err="); Serial.println(err);delay(1000);
    return;
  }

   // converting Celsius to Fahrenheit

  byte f = temperature * 1.8 + 32;  
  Serial.print("Sample OK: ");
  Serial.print((int)temperature); Serial.print(" *C, "); 
  Serial.print( "  ");
    // Serial.print((int)f); Serial.print(" *F, "); 
  Serial.print((int)humidity); Serial.println(" H humidity");
  
  // 
  //  to datafile
   dataFile.println("sen1");
   dataFile.println(",");
   dataFile.println((int)temperature);
   dataFile.println(",");
   dataFile.Println((int)humidity); Serial.println(" H humidity");
   dataFile.println(",");
   
  // Reading data from sensor 2...
  // ============================

  Serial.println("Getting data from sensor 2...");
  
  byte temperature2 = 0;
  byte humidity2 = 0;
  if ((err = dht22.read(dataPinSensor2, &temperature, &humidity, NULL)) != SimpleDHTErrSuccess) {
    Serial.print("Communication error with Sensor 2, err="); Serial.println(err);delay(1000);
    return;
  }

     // converting Celsius to Fahrenheit

  byte fb = temperature2 * 1.8 + 32;
  
  Serial.print("Sample OK: ");
  Serial.print((int)temperature); Serial.print(" *C, "); 
  Serial.print(" ");

  // Serial.print((int)fb); Serial.print(" *F, "); 
  Serial.print((int)humidity); Serial.println(" H humidity");
  Serial.println("  ");
   
//  to datafile
   dataFile.println("sen2");
   dataFile.println(",");
   dataFile.println((int)temperature);
   dataFile.println(",");
   dataFile.Println((int)humidity); Serial.println(" H humidity");
   dataFile.println(",");
   
// DHT11 sampling rate is 1HZ.

delay(8500);
}

./opt/arduino-builder/arduino-builder -compile -core-api-version 10611 -build-path /tmp/486397493/build -hardware opt/arduino-builder/hardware -hardware ./opt/cores -tools opt/arduino-builder/tools -tools ./opt/tools -built-in-libraries opt/libraries/latest -libraries /tmp/486397493/pinned -libraries /tmp/486397493/custom -fqbn arduino:avr:uno -build-cache /tmp -logger humantags -verbose=false /tmp/486397493/2DHT22_Runningand_datalog

Multiple libraries were found for "Logger.h"

Used: /tmp/486397493/pinned/logger-1-0-3

Not used: /home/admin/builder/opt/libraries/latest/logger-1-0-3

/tmp/486397493/2DHT22_Runningand_datalog/2DHT22_Runningand_datalog.ino: In function 'void setup()':

/tmp/486397493/2DHT22_Runningand_datalog/2DHT22_Runningand_datalog.ino:36:13: error: a function-definition is not allowed here before '{' token

/tmp/486397493/2DHT22_Runningand_datalog/2DHT22_Runningand_datalog.ino:109:1: error: expected '}' at end of input

/tmp/486397493/2DHT22_Runningand_datalog/2DHT22_Runningand_datalog.ino:109:1: error: expected '}' at end of input

exit status 1

Where does the setup() function end ?

This line

  while (!Serial) {

probably should not have a curly bracket. Delete the curly bracket.

Then these lines

}
 
Serial.println("card initialized.");

should be swapped. Are there other errors? Possibly.

If you do an Auto Format with Ctrl-T it should be immediately obvious what is wrong. After void setup() it never gets back to the start of a line. The braces {} always have to be in balanced pairs. Yours aren't.

Steve

For future reference:
Common compiler errors caused by mismatched brackets:

"does not name a type" or
"expected declaration before"
Usually means you forgot a '{' or put in an extra '}' in the previous function. Since all of the open brackets have been closed, the compiler is looking for further global declarations (variables or functions). If it finds something that looks like executable code instead of a global declaration it emits an error. Count the brackets in the preceding function declaration.

"a function-definition is not allowed here"
Usually means you forgot a '}' or put in an extra '{' in the previous function. Since a set of brackets has not been closed yet the compiler is looking for more code to put in the function. You can't declare a function inside a function so if the compiler finds a function definition it emits an error. Count the brackets in the preceding function declaration.

"expected '}' at end of input"
Usually means you forgot a '}' or put in an extra '{' in the last function in the sketch. Since a set of brackets has not been closed yet, the compiler is looking for more code to put in the function. When it hits the end of the file instead it emits an error. Count the brackets in the last function declaration.

"expected primary-expression before '}' token"
Usually means you have an incomplete statement before a '}'. The block statement (between '{' and matching '}') can only contain complete statements.

Tnx to all of you.
Got it running and will work on further improvement.
Will try the auto format control-T, and find out if it works online/ web editor or local.

Looked in the taskbar for the debugger