Thread: What is wrong with this program(data structures)

  1. #1
    Unregistered
    Guest

    What is wrong with this program(data structures)

    This is my project for a data structures class using C. I typed out all the functions but i still dont understand why it wont compile can anyone give me any help tips or information thanx. Here is my code.

    Code:
    #include <stdio.h>
    const short STR_MAX = 41;
    
    typedef struct
    {
    	short year;
    	char picture[STR_MAX];
    	char director[STR_MAX];
    } PICTURE;
    
    
    int main (void)
    {
    	void instr      (void);
    	LIST *buildList (void);
    	void process    (LIST *list);
    
    	/*local declaration*/
    	LIST *list;
    
    	/*statement*/
    	instr ();
    	list = buildList ();
    	process (list);
    
    	printf("End Best Pictures\n"
    		   "Hope you found your favorite!\n");
    	return 0;
    }/*main*/
    
    /*=======================instr====================================*/
    void instr (void)
    {
    	/*statements*/
    	printf("This program will print the academy awards,enter year\n");
    	
    	return;
    }/*instr*/
    
    /*=========================buildList==============================*/
    LIST *buildList (void)	
    {
    /*Local Declarations*/
    	FILE *fpData;
    	LIST *list;
    
    	short yearIn;
    	char picIn[STR_MAX];
    	char dirIn[STR_MAX];
    
    	PICTURE *pPic;
    
    /*Statements*/
    	list = createList (cmpYear);
    	if (!list);
    		printf("\aCannot create list\n"),
    			exit(100);
    		fpData = fopen("pictures.dat", "r");
    	if (!fpData)
    		printf("\aError opening input file\n"),
    			exit(100);
    	while (fscanf(fpData," %hd", &yearIn)==1)
    	{
    	if (!(pPic = (PICTURE*)malloc(sizeof(PICTURE))))
    		printf("\aOut of Memorey in build list\n"),
    			exit(100);
    	pPic->year = yearIn;
    
    	/* Skip tabs and quote */
    	while ((fgetc(fpData)) != '\t');
    	while ((fgetc(fpData)) != '"');
    
    	fscanf(fpData, "  %40[^\"], %*c", pPic->picture);
    
    /*skip tabs and quote */
    	while ((fgetc(fpData)) != "\t');
    	while ((fgetc(fpData)) != '"');
    	fscanf(fpData, " %40[^\"], %*c", pPic->director);
    
    /* Insert into list */
    addResult = addNode (list,pPic);
    if (addResult != 0)
    	if (addResult == -1)
    		printf("Memory overflow adding movie\a\n"),
    			exit(120);
    	else
    		printf("Duplicate year %hd not added\n\a",
    								pPic->year);
    
    	while (fgetc(fpData) != '\n');
    	}
    
    	return list;
    } /*buildList*/
    
    /*=============================cmpYear===========================*/
    
    int cmpYear (void *pYear1,
    			 void *pYear2)
    
    {
    	/*local definitions*/
    	int result;
    
    	short year1;
    	short year2;
    
    	/*statements*/
    	year1= ((PICTURE *)pYear1)->year;
    	year2= ((PICTURE *)pYear2)->year;
    
    	if (year1 < year2)
    		result = -1;
    	else if (year1 > year2)
    		result = +1;
    	else 
    		result = 0;
    	return result;
    }/*cmpYear*/
    
    
    
    /*============================process============================*/
    void process (LIST *list)
    {
    	/*statements*/
    	void printList (LIST *list);
    	void search    (LIST *list);
    	char getChoice (void);
    
    	/*local declarations*/
    	char choice;
    
    	/*statements*/
    	do
    	{
    		choice = getChoice ();
    		switch (choice)
    		{
    		case 'P':printList (list);
    			     break;
    		case 'S':search (list);
    		case 'Q':break;
    		}/*switch*/
    	}while (choice !='Q');
    	return;
    }/*process*/
    
    /*==========================getChoice==============================*/
    char getChoice (void)
    {
    	/*localDeclarations*/
    	char choice;
    	int valid;
    	
    	/*statements*/
        printf("================MENU====================\n"
    		   "Here are your choices:\n"
    		   "	S: Search for a year\n"
    		   "	P: Print all years\n"
    		   "	Q: Quit			    \n\n"
    		   "Enter your choice:	");
    	do 
    	{
    		scanf(" %c", &choice);
    		choice = toupper (choice);
    		switch (choice)
    		{
    		case 'S':
    		case 'P':
    		case 'Q': valid = 1;
    			break;
    		default: valid = 0;
    			printf("\aInvalid choice\n"
    				"Please try again: ");
    			break;
    		}/*switch*/
    	} while (!valid);
    	return choice;
    }/*getChoice*/
    
    /*===========================printList===========================*/
    void printList (LIST *list)
    {
    	/*local declarations*/
    	int location;
    
    	PICTURE *pPic;
    
    	/*statements*/
    	/*getFirstNode*/
    	if (listCount (list) == 0)
    		printf("Sorry, nothing in list\n\a");
    	else
    	{
    		printf("\nBest Pictures List\n");
    		traverse (list,0,&pPic);
    		do
    		{
    			printf("%hd %-40s %s\n",
    				pPic->year, pPic->picture, pPic->director);
    		}while (traverse (list,1,&pPic));
    	}/*else*/
    	printf("End of Best Pictures List\n\n");
    }/*printList*/
    
    /*==========================search================================*/
    void search (LIST *list)
    {
    	/*local declarations*/
    	short year;
    	short found;
    
    	PICTURE pSrchArgu;
    	PICTURE *pPic;
    
    	/*statements*/
    	printf("enter a four digit year: ");
    	scanf("%hd", &year);
    	pSrchArgu.year = year;
    
    	found = searchList (list, &pSrchArgu, &pPic);
    
    	if (found)
    		printf("%hd %-40s %s\n",
    		pPic->year,pPic->picture, pPic->director);
    	else
    		printf("Sorry, but %d is not available.\n",year);
    	return;
    }/*search*/

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    1) It would be a lot easier if you printed the errors you got.

    2) You declare these functions inside main, you should move them outside (before main):

    void instr (void);
    LIST *buildList (void);
    void process (LIST *list);
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structures within structures for a database
    By Holtzy in forum C Programming
    Replies: 2
    Last Post: 04-30-2008, 07:06 AM
  2. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 09:04 AM
  3. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  4. Replies: 5
    Last Post: 04-11-2002, 11:29 AM
  5. Methods for Sorting Structures by Element...
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 09-14-2001, 12:59 PM