Thread: error syntax error before {

  1. #1
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252

    error syntax error before {

    Hey guys i have a project that im working on and i have an error everytime i compile.

    It has several files this program so i have attatched the files onto this thread.

    It says on line 222 in ts_utility.c syntax error before or at { error code 2

    Could someone please help me solve it thanks..

    Code:
    /****************************************************************************
    * COSC1098/1283 - Assignment #2 - Tennis Store
    * Programming Principles 2A/Programming Techniques
    * Author           : <insert name here>
    * Student Number   : <insert student number here>
    * Yallara Username : <insert username here>
    * Start up code provided by Steven Burrows - [email protected]
    ****************************************************************************/
    
    #include "ts.h"
    #include "ts_utility.h"
    #include "ts_options.h"
    
    /****************************************************************************
    * Function readRestOfLine() is used for buffer clearing. Source: 
    * https://inside.cs.rmit.edu.au/~pmcd/teaching/C-Prog/CourseDocuments/
    * FrequentlyAskedQuestions/
    ****************************************************************************/
    void readRestOfLine()
    {
       int c;
    
       /* Read until the end of the line or end-of-file. */   
       while ((c = fgetc(stdin)) != '\n' && c != EOF)
          ;
    
       /* Clear the error and end-of-file flags. */
       clearerr(stdin);
    }
    
    /****************************************************************************
    * Initialises the system to a safe empty state.
    ****************************************************************************/
    void systemInit(TennisStoreType* ts)
    {
    }
    
    /****************************************************************************
    * Loads all data into the system.
    ****************************************************************************/
    void loadData(TennisStoreType* ts, char* customerFile, char* stockFile)
    {
    
      /* declaration of variables*/
      char* stockID;
      char* description;
      char* custID;
      char* surname;
      char* firstName;
      char* address;
      char* suburb;
      char sTmp[100];
      char cTmp[100];
      char* unitpricePtr;
      char* stocklevelPtr;
      char* postCodePtr;
      char* phoneNumPtr;
    
      double unitprice;
      int stocklevel;  
      int postCode;
      int phoneNum;
    
    
    
      /* pointing nodes to customer variables*/
    
      CustomerNodeType* newCust;
      CustomerNodeType* curCust;
      CustomerNodeType* prevCust;
    
      /* variables for files*/
    
      FILE *cFile;
      FILE *sFile;
    
      if((cFile = fopen(customerFile, "r")) == NULL || (sFile = fopen(
      stockFile,"r")) == NULL)
      {
        fprintf(stderr, "error opening file\n");
        exit(EXIT_FAILURE);
    
      }
      else
      {
    
        printf("file was opened correctly");
      }
    
      while(fgets(cTmp, 100, cFile ) != NULL)
      {
       
        /* tokenizing variables*/
    
        custID = strtok(cTmp, ",");
        surname = strtok(NULL, ",");
        firstName = strtok(NULL, ",");
        suburb = strtok(NULL, ",");
        postCodePtr = strtok(NULL, ",");
        phoneNumPtr = strtok(NULL,"/0");
        postCode = atoi(postCodePtr);
        phoneNum = atoi(phoneNumPtr);
    
        /* allocating memory using malloc for nodes*/        
       
        prevCust = malloc(sizeof(*prevCust));
        curCust = malloc(sizeof(*curCust));
        newCust = malloc(sizeof(*newCust));
    
        /* start of linked list*/
    
        curCust = ts -> headCust;
        prevCust = NULL;
        
        while(curCust !=NULL)
        {
           prevCust = curCust;
           
           curCust = curCust -> nextCust;
    
        }
        /* copy the string variables into newCust node*/
    
        strcpy(newCust->custID,custID);
        strcpy(newCust->surname,surname);
        strcpy(newCust->firstName,firstName);
        strcpy(newCust->address,address);
        strcpy(newCust->suburb,suburb);
    
        /* newCust is postCode*/
    
        newCust->postCode=postCode;
    
        /* newCust is phoneNum*/
    
        newCust->phoneNum=phoneNum;
       
        /* printing the preloaded data*/
        printf("\n\t%s",newCust -> custID);
        printf("\t%s",  newCust -> firstName);
        printf("\t%s",  newCust -> surname);
        printf("\t%s",  newCust -> address);
        printf("\t%s",  newCust -> suburb);
        printf("\t%d",  newCust -> postCode);
        printf("\t%d",  newCust -> phoneNum);
    
        /* keep count of each customer in loop*/
    
        ts -> customerCount++;
    
        /* start of linked list*/
    
        if(prevCust == NULL)
        {
          ts ->headCust = newCust;
    
        }
        else
        {
       
          prevCust -> nextCust = newCust;
    
        }
        /* if there is a previous node get the next node and make
           it the new node*/
      }
      fclose(sFile);
    
      while(fgets(sTmp, 100, sFile) != NULL)
      {
        /* tokenizing each field from comma*/
    
        stockID = strtok(sTmp, ",");
        description = strtok(NULL, ",");
        unitpricePtr = strtok(NULL,",");
        stocklevelPtr = strtok(NULL, "/0");
       
        /* cast the Ptr character to an integers
     
        unitprice = atoi(unitpricePtr);
        stocklevel = atoi(stocklevelPtr);
    
        prevStock = malloc(sizeof(*prevStock));
        curStock = malloc(sizeof(*curStock));
        newStock = malloc(sizeof(*newStock));
    
        curStock = ts -> headStock;
        prevStock = NULL;
    
        while(curStock != NULL)
        {
           prevStock = curStock;
           
           curStock = curStock -> nextStock;
    
        }
    
        ts -> stockCount++;
    
        if(prevStock == NULL)
        {
           ts->headStock=newStock;
        }
        else
        {
           prevStock->nextStock=newStock;
        }
       
    
      }
      fclose(sFile);
     
    
    
    
    }
    
    /****************************************************************************
    * Deallocates memory used in the program.
    ****************************************************************************/
    void systemFree(TennisStoreType* ts)
    {
    
    
    }

    I couldnt submit the exstra file ts_utility.h
    Code:
    #ifndef TS_UTILITY_H
    #define TS_UTILITY_H
    
    /* Function prototypes. */
    void readRestOfLine();
    void systemInit(TennisStoreType* ts);
    void loadData(TennisStoreType* ts, char* customerFile, char* stockFile);
    void systemFree(TennisStoreType* ts);
    
    #endif

  2. #2
    FOX
    Join Date
    May 2005
    Posts
    188
    You forgot to close the comment on line 178... A good editor would've picked it up instantly as the rest of the code would be colored in the comment color.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Guess it's this line
    Code:
        /* cast the Ptr character to an integers
    But next time don't let me count to 222.
    Kurt

  4. #4
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    ohhh no joke silly silly thanks for that

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Great... and what line is line 222? ...or do we actually have to count ourselves line by line? I did notice one thing that is a problem: See below in red; you are closing sFile prematurely, I think you meant cFile.

    Code:
    void loadData(TennisStoreType* ts, char* customerFile, char* stockFile)
    {
    
      /* declaration of variables*/
      char* stockID;
      char* description;
      char* custID;
      char* surname;
      char* firstName;
      char* address;
      char* suburb;
      char sTmp[100];
      char cTmp[100];
      char* unitpricePtr;
      char* stocklevelPtr;
      char* postCodePtr;
      char* phoneNumPtr;
    
      double unitprice;
      int stocklevel;  
      int postCode;
      int phoneNum;
    
    
    
      /* pointing nodes to customer variables*/
    
      CustomerNodeType* newCust;
      CustomerNodeType* curCust;
      CustomerNodeType* prevCust;
    
      /* variables for files*/
    
      FILE *cFile;
      FILE *sFile;
    
      if((cFile = fopen(customerFile, "r")) == NULL || (sFile = fopen(
      stockFile,"r")) == NULL)
      {
        fprintf(stderr, "error opening file\n");
        exit(EXIT_FAILURE);
    
      }
      else
      {
    
        printf("file was opened correctly");
      }
    
      while(fgets(cTmp, 100, cFile ) != NULL)
      {
       
        /* tokenizing variables*/
    
        custID = strtok(cTmp, ",");
        surname = strtok(NULL, ",");
        firstName = strtok(NULL, ",");
        suburb = strtok(NULL, ",");
        postCodePtr = strtok(NULL, ",");
        phoneNumPtr = strtok(NULL,"/0");
        postCode = atoi(postCodePtr);
        phoneNum = atoi(phoneNumPtr);
    
        /* allocating memory using malloc for nodes*/        
       
        prevCust = malloc(sizeof(*prevCust));
        curCust = malloc(sizeof(*curCust));
        newCust = malloc(sizeof(*newCust));
    
        /* start of linked list*/
    
        curCust = ts -> headCust;
        prevCust = NULL;
        
        while(curCust !=NULL)
        {
           prevCust = curCust;
           
           curCust = curCust -> nextCust;
    
        }
        /* copy the string variables into newCust node*/
    
        strcpy(newCust->custID,custID);
        strcpy(newCust->surname,surname);
        strcpy(newCust->firstName,firstName);
        strcpy(newCust->address,address);
        strcpy(newCust->suburb,suburb);
    
        /* newCust is postCode*/
    
        newCust->postCode=postCode;
    
        /* newCust is phoneNum*/
    
        newCust->phoneNum=phoneNum;
       
        /* printing the preloaded data*/
        printf("\n\t%s",newCust -> custID);
        printf("\t%s",  newCust -> firstName);
        printf("\t%s",  newCust -> surname);
        printf("\t%s",  newCust -> address);
        printf("\t%s",  newCust -> suburb);
        printf("\t%d",  newCust -> postCode);
        printf("\t%d",  newCust -> phoneNum);
    
        /* keep count of each customer in loop*/
    
        ts -> customerCount++;
    
        /* start of linked list*/
    
        if(prevCust == NULL)
        {
          ts ->headCust = newCust;
    
        }
        else
        {
       
          prevCust -> nextCust = newCust;
    
        }
        /* if there is a previous node get the next node and make
           it the new node*/
      }
      fclose(sFile);
    
      while(fgets(sTmp, 100, sFile) != NULL)
      {
        /* tokenizing each field from comma*/
    
        stockID = strtok(sTmp, ",");
        description = strtok(NULL, ",");
        unitpricePtr = strtok(NULL,",");
        stocklevelPtr = strtok(NULL, "/0");
       
        /* cast the Ptr character to an integers
     
        unitprice = atoi(unitpricePtr);
        stocklevel = atoi(stocklevelPtr);
    
        prevStock = malloc(sizeof(*prevStock));
        curStock = malloc(sizeof(*curStock));
        newStock = malloc(sizeof(*newStock));
    
        curStock = ts -> headStock;
        prevStock = NULL;
    
        while(curStock != NULL)
        {
           prevStock = curStock;
           
           curStock = curStock -> nextStock;
    
        }
    
        ts -> stockCount++;
    
        if(prevStock == NULL)
        {
           ts->headStock=newStock;
        }
        else
        {
           prevStock->nextStock=newStock;
        }
       
    
      }
      fclose(sFile);
     
    
    
    
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed