Hello all,

When I try to compile a small program I am getting an error that I do not quite understand.

I get this error:
media_tracker1.c: In function 'add':
media_tracker1.c:152: error: expected expression before ';' token

when I try to compile this code:

#include <stdio.h>
#include <stdlib.h>
#define HLIMIT 120
#define MLIMIT 50
#define LLIMIT 6
#define DATE_SIZE 14
#define NUM_CATS 7;

//Media Map
//Author: xxx xxx
//
//Project 1

typedef struct mediaItem_ {
	char doc[DATE_SIZE]; //date of creation
	int  size;
	char desc[MLIMIT];
	short category;
	char location[HLIMIT];
	char creator[MLIMIT];
} mediaItem;

mediaItem *items = NULL;
int	num_items = 0;
int num_allocated = 0;
char *catArray[] ={"OS", "APP", "FILM", "MUSIC", "MUSIC VIDEO", "VIDEO GAME", "OTHER"};


//helper functions
//----------------

// addItems is an adaptation of the AddToArray function
// found at http://fydo.net/gamedev/dynamic-arrays
int addItems (mediaItem item){
	if(num_items == num_allocated){
		if(num_allocated == 0)
			num_allocated = 8;
		else
			num_allocated += num_allocated/4;

		void *_tmp = realloc(items, (num_allocated * sizeof(mediaItem)));

		//error case
		if(!_tmp){
			fprintf(stderr,
				"ERROR: Unable to allocate more memory for additonal items\n");
			return(-1);
		}

		//success case
		items = (mediaItem*)_tmp;
	}
	items[num_items++] = item;

	return num_items;
}

void intro(){
    printf("Hello and welcome to Media Map\n\n");
}

void bye(){
    printf("\nThank you for using Media Map!\n");
}

void show_menu(){
    printf("\nPlease select from the following choices:\n");
    printf("1. List Media\n");
    printf("2. Add Media\n");
    printf("3. Delete Media\n");
    printf("4. Exit\n");
}

char* getString(char *string, int length){
	fgets(string, length, stdin);
}

int getSize(){
	char *input;
	int  nbytes = LLIMIT + 2;
	int  size;
	getline(&input, &nbytes, stdin);
	if(sscanf(input, "%d", &size) == 1)
		return size;
	
	return -1;
}


//selection functions
//-------------------

void list(){
	printf("\nList Selected\n");
	printf("########## LIST START ##########\n");

	int i;
	for(i=0; i < num_items; ++i){
		if(num_items == 0)
			printf("\n\tThere are no media items to list.\n\n");
		if(i == num_items){ break; }

		mediaItem temp = *(items + i);
		printf("***** START RECORD %d *****\n", i);

		printf("Descript:    %s", temp.desc);
		printf("Location:    %s", temp.location);
		printf("Creator:     %s", temp.creator);
		printf("Category:    %s", temp.category);
		printf("Size:        %dMB", temp.size);
		printf("Created:     %s", temp.doc);

		printf("*****  END RECORD %d  *****\n", i);
	}
	printf("##########  LIST END  ##########\n");
}

void add(){
	mediaItem *item;
	
	if((item = (mediaItem *) malloc(sizeof(mediaItem))) == NULL){
		printf("ERROR ALLOCATING MEMORY FOR NEW ITEM\n");
		exit;
	}

	printf("\nAdd Selected\n\n");

	printf("Enter the description of the media\n");
	getString(item->desc, HLIMIT);
	
	printf("Enter the location of the media\n");
	getString(item->desc, MLIMIT);
	
	printf("Enter a creator for the media\n");
	getString(item->creator, MLIMIT);
	
	printf("Enter the size of the media (in MB)\n");
	item->size = -1;
	do{
		item->size = getSize();
		if(item->size < 0)
			printf("Size must be entered as an integer. Please try again.\n");
	} while(item->size < 0);

	printf("Enter the creation time of the media\n");
	getString(item->doc, DATE_SIZE);
	
	item->category = -1;
	int i;
	do{
		printf("Choose number matching the category of the media\n");
		for(i = 0; i < NUM_CATS; ++i)
			 printf("%d = %s", i, catArray[i]);
		item->size = getSize();
		if(item->category < 0)
			printf("Please enter a number corresponding to category list.\n");
	} while(item->category < 0);
	printf("\n");
	
	addItems(*item);
	free(item);
}

void del(){
    printf("\nDelete Selected - Replace Stub\n");
	printf("\n");
}

void exit_func(){
    printf("\nExit Selected\n");
	free(items);
}

void bad_selection(char *value){
	if(value[0] == '\n')
        printf("No selection detected. Please try again.\n");
	else
		printf("\n\'%s\' is not a valid response. Please try again.\n", value);
}


//program
//-------

int main(){
	int nbytes = 3; // for some reason crashes if set to 2
    int selection = 0;
	char *input;
	
    while(selection != 4){
        show_menu();
		//input = (char *) malloc(nbytes + 1);
		getline(&input, &nbytes, stdin);
		input[1] = '\0';
		if(sscanf(input, "%d", &selection) == 1){
			switch(selection){
				case 1:
					list(); break;
				case 2:
					add(); break;
				case 3:
					del(); break;
				case 4:
					exit_func(); break;
				default:
					bad_selection(input); break;
			}
		} else { bad_selection(input);}
	}

	bye();

	return 0;
}

I am by no means a C guru (as you can probably tell from the code), but line 152 is simply part of a for loop... what expression am I missing here?

SIDE NOTE:
note sure if I should start a whole new thread for this but I was wondering about line 162. Is that a bad place to free up the memory for the mediaItem? I'm thinking that if C passes by value then a duplicate is being created for the array, so this should be fine. Am I off base here? Would I be deallocating memory of the very same item I just added to the array?

Line 149, C doesn't allow you to declare variables in the middle of code.

This is a feature of C99, but most people still use C89 for programming.

Thanks for the reply Salem. Unfortunately I get the exact same error message when I make the declaration "int i;" at the top of the function. So something else afoot.

Any more ideas?

Thanks for the reply Salem. Unfortunately I get the exact same error message when I make the declaration "int i;" at the top of the function. So something else afoot.

Any more ideas?

Maybe you need to point out the exact line. Your compiler may not match the numbering on the forum.

Hi Walt, I double checked when I posted. Both the form and my editor are in sync as far as line numbering is concerned.

But to be clear, the line that the compiler is complaining about is the one that reads:

for(i = 0; i < NUM_CATS; ++i)
#define NUM_CATS 7;

Take off the ;

commented: +++ +19
#define NUM_CATS 7;

Take off the ;

DOH! Thanks sysop_fb! It's all ways the simplest things!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.