Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   parse error ansi c with struct (http://www.go4expert.com/forums/parse-error-ansi-c-struct-t789/)

musicmancanora4 7May2006 12:11

parse error ansi c with struct
 
I get an error saying parse error before category Type on line 258 not sure why
currentItem = CategoryType->headItem;
im trying to set the headItem to the start of the linked list there is my structs
and my load function below i would appreciate the help thanks.

Code:

typedef struct category* CategoryTypePtr;
typedef struct item* ItemTypePtr;

/* Structure definitions. */
typedef struct price
{
  unsigned dollars;
  unsigned cents;
} PriceType;

typedef struct item
{
  char itemID[ID_LEN + 1];
  char itemName[MAX_NAME_LEN + 1];
  PriceType prices[NUM_PRICES];
  char itemDescription[MAX_DESC_LEN];
  ItemTypePtr nextItem;
} ItemType;

typedef struct category
{
  char categoryID[ID_LEN + 1];
  char categoryName[MAX_NAME_LEN + 1];
  char drinkType;      /* (H)ot or (C)old. */
  char categoryDescription[MAX_DESC_LEN];
  CategoryTypePtr nextCategory;
  ItemTypePtr headItem;
  unsigned numItems;
} CategoryType;

typedef struct gjc
{
  CategoryTypePtr headCategory;
  unsigned numCategories;
} GJCType;

int loadData(GJCType* menu, char* menuFile, char* submenuFile)


Code:

int loadData(GJCType* menu, char* menuFile, char* submenuFile)
{
  /*pointer for malloc*/
  CategoryType *newCat, *currentCat, *prevCat ;
  ItemType    *newItem, *currentItem, *prevItem;
 
 

  /*declaration of variables*/
  int CountNoLines = 0;
  char temp[LINE_LENGTH];
  char tempf2[LINE_LENGTH];
 
  /* variables for tokenizer for menu file*/
  char *catID;
  char *catType;
  char *catName;
  char *catDescription;
  char *line;
 
  int count;
 
  /* variables for sub menu*/
  char *itemIDPtr;
  char *itemNamePtr;
  char *itemDescriptionPtr;
  char *dollarPtr;
  char *centsPtr;
 
  float dollarInt;
  float centInt;
 
  /* declare file pointers to files*/
  FILE *f1;
  FILE *f2;
 
 
  /* opening the menu and submenu for reading*/
  f1 = fopen(menuFile, "r");
  f2 = fopen(submenuFile, "r");
 
  /* check to see if the filuse exists*/
  if(f1 ==NULL || f2 == NULL)
  {
      /* check whether both files have existed or typed in correctly*/
      if(f1 == NULL && f2 == NULL)
      {
     
        printf("Both files do not exist %s %s\n",menuFile,submenuFile);
      }
      /* check for each file existance*/
      else if (f1 == NULL)
      {
        printf("%s does not exist\n\n",menuFile);
      }
      /* check for each file existance*/
      else if(f2 == NULL)
      {
        printf("%s does not exist\n\n", submenuFile);
       
      }
      printf("EXITING PROGRAM ERROR!\n\n");
      return ERRORCODE; /* cannot proceed*/
  }
 
  /* counts how many fields there are in the file*/
 
  while(fgets(temp, LINE_LENGTH +2, f1)!=NULL)
  {
         
         
          currentCat = menu->headCategory;
 
          newCat = malloc(sizeof(CategoryType));
         
         
          if(prevCat == NULL)
          {
              menu->headCategory = newCat;
             
         
          }
          /* else
          {
              prevCat->nextCategory = newCat;
             
          */
 
          /* stores the catID but also checks if there
          is a duplicate*/
           
          catID = strtok(temp, "|");
         
          /* checking whether it tokenizez*/
        /*  if(catID == NULL)
          {
              printf("CatID missing\n");
          }*/
          strcpy(newCat->categoryID, catID);
         
         
          /*checks for a duplication of the id field*/
          if(!checkDuplicationID(menu, catID))
          {
              return ERRORCODE;
          }
          /* gets the second field and tokenizez it*/
          catType = strtok(NULL, "|");
          newCat->drinkType = catType[0];
         
            /* checking whether it tokenizez*/
          if(catType == NULL)
          {
              printf("CatType missing\n");
          }
         
         
          catName = strtok(NULL, "|");
          strcpy(newCat->categoryName, catName);
            /* checking whether it tokenizez*/
           
          if(catName == NULL)
          {
              printf("CatName missing\n");
          }
         
         
          catDescription = strtok(NULL, "\n");
          strcpy(newCat->categoryDescription, catDescription);
            /* checking whether it tokenizez*/
          if(catDescription == NULL)
          {
              printf("CatDescription missing\n");
          }
         
          /* checks the string length of the field*/
          if((strlen(catID) >ID_LEN) ||
              (strlen(catType) >MAX_NAME_LEN)  ||
              (strlen(catDescription) >MAX_DESC_LEN))
             
          {         
         
              printf("Wrong data format\n\n");
              return ERRORCODE;
             
          }
         
         
         
         
          /* put the pointer to the start of the list*/
         
          /*newCat->nextCategory = NULL;
            newCat->headItem = NULL;
            newCat->numItems = 0;
            prevCat = newCat;*/
            /*
            currentCat = menu->headCategory;
            prevCat = NULL;
           
         
           
            if(prevCat == NULL)
            {
              menu->headCategory = newCat;
              newCat->nextCategory = currentCat;
           
            }
            else
            {
              prevCat->nextCategory=newCat;
              newCat->nextCategory = currentCat;
            }*/
           
         
           
         
              while(currentCat !=NULL)
          {
              printf("%s %c %s %s\n", currentCat->categoryID,
              currentCat->drinkType,currentCat->categoryName,
              currentCat->categoryDescription);
             
              currentCat = currentCat->nextCategory;
         
          }
         
       
         
  }  /*end of while loop*/ 
     
  while(fgets(tempf2, LINE_LENGTH +2, f1)!=NULL)
  {
         
         
          currentItem = CategoryType->headItem;
 
          newItem = malloc(sizeof(ItemType));
         
         
          if(prevItem == NULL)
          {
            /*  menu->headItem = newItem;*/
             
         
          }
       
         
           
         
         
       
         
  }  /*end of while loop*/ 
 
  /* close both files after reading*/
  if(fclose(f1)!=0 || fclose(f2)!=0)
  {
      fprintf(stderr, "Error in closing files\n");
  } 
 

  return EXIT_SUCCESS;
}


shabbir 7May2006 13:49

Re: parse error ansi c with struct
 
Can you put the complete source code so that we can compile and see. In the above code you have lots of Macros that needs to be defined for successful compilation.

musicmancanora4 7May2006 16:36

Re: parse error ansi c with struct
 
its a fairly large program around 6 files should i upload the files instead?

shabbir 7May2006 18:41

Re: parse error ansi c with struct
 
6 .c/.cpp/.h text files after zipping will not be much but if you think its becoming too much size you can email me as well at shabbir[@]go4expert.com

musicmancanora4 7May2006 19:08

Re: parse error ansi c with struct
 
Thanks Shabir for looking at it


Code:

#ifndef GJC_UTILITY_H
#define GJC_UTILITY_H

/* Function prototypes. */
void readRestOfLine();
void systemInit(GJCType* menu);
int loadData(GJCType* menu, char* menuFile, char* submenuFile);
void systemFree(GJCType* menu);
int checkNumInput(char input[]);
#endif


Code:

#include "gjc.h"
#include "gjc_options.h"
#include "gjc_utility.h"

/****************************************************************************
* Function readRestOfLine() is used for buffer clearing. Source:
* https://inside.cs.rmit.edu.au/~sdb/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(GJCType* menu)
{
 

   
  /* declaration of for loop variables*/
  int i;
 
  /* remember to malloc all pointers in structs*/
 
  menu->headCategory = NULL;
 
  menu->numCategories = 0;
 
 
 
 
 
 
}


/****************************************************************************
* Loads all data into the system.
***********************
*****************************************************/
int loadData(GJCType* menu, char* menuFile, char* submenuFile)
{
  /*pointer for malloc*/
  CategoryType *newCat, *currentCat, *prevCat ;
  ItemType    *newItem, *currentItem, *prevItem;
 
 

  /*declaration of variables*/
  int CountNoLines = 0;
  char temp[LINE_LENGTH];
  char tempf2[LINE_LENGTH];
 
  /* variables for tokenizer for menu file*/
  char *catID;
  char *catType;
  char *catName;
  char *catDescription;
  char *line;
 
  int count;
 
  /* variables for sub menu*/
  char *itemIDPtr;
  char *itemNamePtr;
  char *itemDescriptionPtr;
  char *dollarPtr;
  char *centsPtr;
 
  float dollarInt;
  float centInt;
 
  /* declare file pointers to files*/
  FILE *f1;
  FILE *f2;
 
 
  /* opening the menu and submenu for reading*/
  f1 = fopen(menuFile, "r");
  f2 = fopen(submenuFile, "r");
 
  /* check to see if the filuse exists*/
  if(f1 ==NULL || f2 == NULL)
  {
      /* check whether both files have existed or typed in correctly*/
      if(f1 == NULL && f2 == NULL)
      {
     
        printf("Both files do not exist %s %s\n",menuFile,submenuFile);
      }
      /* check for each file existance*/
      else if (f1 == NULL)
      {
        printf("%s does not exist\n\n",menuFile);
      }
      /* check for each file existance*/
      else if(f2 == NULL)
      {
        printf("%s does not exist\n\n", submenuFile);
       
      }
      printf("EXITING PROGRAM ERROR!\n\n");
      return ERRORCODE; /* cannot proceed*/
  }
 
  /* counts how many fields there are in the file*/
 
  while(fgets(temp, LINE_LENGTH +2, f1)!=NULL)
  {
         
         
          currentCat = menu->headCategory;
 
          newCat = malloc(sizeof(CategoryType));
         
         
          if(prevCat == NULL)
          {
              menu->headCategory = newCat;
             
         
          }
          /* else
          {
              prevCat->nextCategory = newCat;
             
          */
 
          /* stores the catID but also checks if there
          is a duplicate*/
           
          catID = strtok(temp, "|");
         
          /* checking whether it tokenizez*/
        /*  if(catID == NULL)
          {
              printf("CatID missing\n");
          }*/
          strcpy(newCat->categoryID, catID);
         
         
          /*checks for a duplication of the id field*/
          if(!checkDuplicationID(menu, catID))
          {
              return ERRORCODE;
          }
          /* gets the second field and tokenizez it*/
          catType = strtok(NULL, "|");
          newCat->drinkType = catType[0];
         
            /* checking whether it tokenizez*/
          if(catType == NULL)
          {
              printf("CatType missing\n");
          }
         
         
          catName = strtok(NULL, "|");
          strcpy(newCat->categoryName, catName);
            /* checking whether it tokenizez*/
           
          if(catName == NULL)
          {
              printf("CatName missing\n");
          }
         
         
          catDescription = strtok(NULL, "\n");
          strcpy(newCat->categoryDescription, catDescription);
            /* checking whether it tokenizez*/
          if(catDescription == NULL)
          {
              printf("CatDescription missing\n");
          }
         
          /* checks the string length of the field*/
          if((strlen(catID) >ID_LEN) ||
              (strlen(catType) >MAX_NAME_LEN)  ||
              (strlen(catDescription) >MAX_DESC_LEN))
             
          {         
         
              printf("Wrong data format\n\n");
              return ERRORCODE;
             
          }
         
         
         
         
          /* put the pointer to the start of the list*/
         
          /*newCat->nextCategory = NULL;
            newCat->headItem = NULL;
            newCat->numItems = 0;
            prevCat = newCat;*/
            /*
            currentCat = menu->headCategory;
            prevCat = NULL;
           
         
           
            if(prevCat == NULL)
            {
              menu->headCategory = newCat;
              newCat->nextCategory = currentCat;
           
            }
            else
            {
              prevCat->nextCategory=newCat;
              newCat->nextCategory = currentCat;
            }*/
           
         
           
         
              while(currentCat !=NULL)
          {
              printf("%s %c %s %s\n", currentCat->categoryID,
              currentCat->drinkType,currentCat->categoryName,
              currentCat->categoryDescription);
             
              currentCat = currentCat->nextCategory;
         
          }
         
       
         
  }  /*end of while loop*/ 
     
  while(fgets(tempf2, LINE_LENGTH +2, f1)!=NULL)
  {
         
         
          currentItem = CategoryType->headItem;
 
          newItem = malloc(sizeof(ItemType));
         
         
          if(prevItem == NULL)
          {
            /*  menu->headItem = newItem;*/
             
         
          }
       
         
           
         
         
       
         
  }  /*end of while loop*/ 
 
  /* close both files after reading*/
  if(fclose(f1)!=0 || fclose(f2)!=0)
  {
      fprintf(stderr, "Error in closing files\n");
  } 
 

  return EXIT_SUCCESS;
}


/****************************************************************************
* Deallocates memory used in the program.
****************************************************************************/
void systemFree(GJCType* menu)
{

}

/*this function counts the number of tokens each line has*/
int countToken(FILE *fp, char* temp, int tokenPerLine)
{
  int countToken = 0;
 
  strtok(temp, "|");
  countToken++;
 
 
  /* while there are | delimeters count how many
  there are*/
 
  while(strtok(NULL, "|")!=NULL)
  {
      countToken++;
   
  }
 
  /* if its not a | delimeter then its the wrong data
  format*/
 
  if(countToken != tokenPerLine )
  {
      printf("\nWrong data format \n\n");
     
      return ERRORCODE;
 
  }
  return 1 ;/*countToken;*/

}


Code:

#ifndef GJC_OPTIONS_H
#define GJC_OPTIONS_H

/* Function prototypes. */
void displaySummary(GJCType* menu, char categoryType);
void categoryReport(GJCType* menu);
void addCategory(GJCType* menu);
void deleteCategory(GJCType* menu);
void addItem(GJCType* menu);
void deleteItem(GJCType* menu);
void saveData(GJCType* menu, char* menuFile, char* submenuFile);
int checkDuplicationID(GJCType *menu, char* catID);
int valid(char *p);
int checkNumInput(char input[]);
#endif


Code:

#include "gjc.h"
#include "gjc_options.h"
#include "gjc_utility.h"

/****************************************************************************
* Menu option #1: Display Summary
* Allows the user to display a summary of all hot or cold drink categories
* and items.
****************************************************************************/
void displaySummary(GJCType* menu, char categoryType)
{
}


/****************************************************************************
* Menu option #2: Category Report
* Allows the user to make a new report file for a chosen category.
****************************************************************************/
void categoryReport(GJCType* menu)
{
}


/****************************************************************************
* Menu option #3: Add Category
* Allows the user to add a new category record to the linked list.
****************************************************************************/
void addCategory(GJCType* menu)
{
}


/****************************************************************************
* Menu option #4: Delete Category
* Deletes a category and all corresponding items.
****************************************************************************/
void deleteCategory(GJCType* menu)
{
}


/****************************************************************************
* Menu option #5: Add Item
* Allows the user to add a new item to an existing category. An error
* message is given if the category doesn't exist.
****************************************************************************/
void addItem(GJCType* menu)
{
}


/****************************************************************************
* Menu option #6: Delete Item
* Deletes a single item from a particular category.
****************************************************************************/
void deleteItem(GJCType* menu)
{
}


/****************************************************************************
* Menu option #7: Save and Exit
* Saves all system data back to the original files. This function does not
* terminate the program - this is left to the main() function instead.
****************************************************************************/
void saveData(GJCType* menu, char* menuFile, char* submenuFile)
{
}

/* checks for duplication of fields the catID field*/
int checkDuplicationID(GJCType *menu, char* catID)
{
    CategoryTypePtr current;
    current = NULL;
   
    current = menu->headCategory;
   
   
    while(current !=NULL)
    {
        if(strcmp(current->categoryID, catID) == ERRORCODE)
        {
          printf("A duplicate record was found error:\n\n");
          return ERRORCODE;
        }
       
        current = current->nextCategory;
    }
   
    return 1;
}


Code:

#include "gjc.h"
#include "gjc_options.h"
#include "gjc_utility.h"

int main(int argc, char* argv[])
{
  /* declaration of variables*/
  GJCType menu;
 
 
  int cmd;
  char *menuFile;
  char *submenuFile;
  int openData;
 
  /* to check how many command line aguments entered*/
  commandLineArguments(argc,argv);
 
 
  /* populating arguments with pointers to files*/
  menuFile = argv[1];
  submenuFile = argv[2];
 
  /*initialize variables and pointers*/
  systemInit(&menu);
 
  /* populate the data in the files into memory*/
 
  openData = loadData(&menu, menuFile, submenuFile);
 
  /* if it does not open release the memory*/
  if(!openData)
  {
      /*systemFree(&menu);*/
      return EXIT_SUCCESS;
  }

  return EXIT_SUCCESS;
}

int commandLineArguments(int argc, char *argv[])
{
 
   
  /* command line argument*/
 
  /* checks to see if 3 command line arguments have been entered*/
  if(argc<3)
  {
   
    printf("Invalid: Enter 3 command line arguments (Error:)\n");
    exit(ERRORCODE);
  }
  else if(argc >3)
  {
    printf("Too many arguments where supplied\n");
    return ERRORCODE;
  }
 
 
 
  return EXIT_SUCCESS;

}


Code:

#ifndef GJC_H
#define GJC_H

/* System-wide header files. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* System-wide constants. */
#define ID_LEN 5
#define MIN_NAME_LEN 1
#define MAX_NAME_LEN 25
#define MIN_DESC_LEN 1
#define MAX_DESC_LEN 250
#define NUM_PRICES 3
#define HOT 'H'
#define COLD 'C'
#define ERRORCODE 1
#define VALID 0
#define LINE_LENGTH 500
#define TOKEN_PRODUCT 4

typedef struct category* CategoryTypePtr;
typedef struct item* ItemTypePtr;

/* Structure definitions. */
typedef struct price
{
  unsigned dollars;
  unsigned cents;
} PriceType;

typedef struct item
{
  char itemID[ID_LEN + 1];
  char itemName[MAX_NAME_LEN + 1];
  PriceType prices[NUM_PRICES];
  char itemDescription[MAX_DESC_LEN];
  ItemTypePtr nextItem;
} ItemType;

typedef struct category
{
  char categoryID[ID_LEN + 1];
  char categoryName[MAX_NAME_LEN + 1];
  char drinkType;      /* (H)ot or (C)old. */
  char categoryDescription[MAX_DESC_LEN];
  CategoryTypePtr nextCategory;
  ItemTypePtr headItem;
  unsigned numItems;
} CategoryType;

typedef struct gjc
{
  CategoryTypePtr headCategory;
  unsigned numCategories;
} GJCType;

int commandLineArguments(int argc, char* argv[]);
int countToken(FILE *fp, char* temp, int tokenPerLine);
#endif


shabbir 8May2006 22:35

Re: parse error ansi c with struct
 
The error is you are trying to use the -> operator of the class but it should be of the object.

CategoryType is a class and its objects / pointers like *newCat, *currentCat, *prevCat should be used here

Adi_2021 24Sep2008 18:15

Re: parse error ansi c with struct
 
hey musicmancanora4. could you please paste the completed code. trying to understand linked lists....would be a great help..thanks.


All times are GMT +5.5. The time now is 07:37.