- #1
Epic Sandwich
- 25
- 0
I'm following a tutorial off of the internet - http://masters-of-the-void.com/book10.htm" one to be exact - and whenever I try and compile I get this error:
[PLAIN]http://dl.dropbox.com/u/1426380/Screen%20shot%202010-08-01%20at%2015.26.11.png
I've tried going over my code again and again, but seeing as it doesn't seem to tell me where the error is happening, it's really difficult for me (as a beginner with C) to actually find the problem.
I'll post all my code below to help you out.
main.c:
myFunctions.c:
main.h:
I think it's something to do with the data structure not being declared properly, I've tried doing different things with it but none of them have got rid of the error.
I apologise if I've done something completely wrong here, the lesson that I was following was a bit vague at times, and i wasn't that sure on where to put things in terms of header files etc.
Thanks.
[PLAIN]http://dl.dropbox.com/u/1426380/Screen%20shot%202010-08-01%20at%2015.26.11.png
I've tried going over my code again and again, but seeing as it doesn't seem to tell me where the error is happening, it's really difficult for me (as a beginner with C) to actually find the problem.
I'll post all my code below to help you out.
main.c:
Code:
#include "main.h"
#include "myFunctions.c"
int numDatabaseEntries = 0;
struct DatabaseEntry * gDatabase = NULL;
int main() //Main program function
{
bool keepRunning = true; //Declares and assigns variable to allow program to loop
char userInput[11]; //String for user input
printf("Welcome to the CD Database!\n"); //Welcome message. Only displayed once as it is outside of the 'while' block
while ( keepRunning == true ) //Keeps program looping while the keepRunning variable is true
{
printf( "\nType NEW, LIST, CLEAN or QUIT:\n>" );
scanf( "%5s", userInput );
fpurge( stdin );
if ( strcmp( userInput, "NEW" ) == 0 )
NewEntry(); //Code for new database entry
else if ( strcmp( userInput, "LIST" ) == 0 )
List(); //Code for list database
else if ( strcmp( userInput, "CLEAN" ) == 0 )
CleanUp(); //Code for cleanup
else if( strcmp( userInput, "QUIT" ) == 0 )
{
keepRunning = false; //Ends program by exiting out of the loop
CleanUp(); //Frees memory before quitting
}
else
printf( "Error, command '%s' is unknown.", userInput ); //Prints unknown command to warn user
}
printf("\nThank you for using the CD Database.");
return 0; //Returns ok value to OS
}
myFunctions.c:
Code:
#include "main.h"
struct DatabaseEntry //Defines custom data structure to use to store information on each CD
{
char artist[40];
char composer[40];
char albumName[40];
int trackCount;
bool isSampler;
};
void NewEntry() //Function used for entering a new entry to database
{
char yesOrNo; //For 'is sampler' later on
//Create new array element, or new array if no array exists
if ( gDatabase == NULL )
{
gDatabase = malloc( sizeof(struct DatabaseEntry) );
if ( gDatabase == NULL ) //Still null? Error must have occured
{
printf( "Error, couldn't create a new entry.\n" );
return;
}
}
else
{
struct DatabaseEntry * newPtr = NULL; //Declare temporary Database holder to use realloc with
newPtr = realloc( gDatabase, ( numDatabaseEntries + 1 ) * sizeof( struct DatabaseEntry ) ); //Make newPtr one unit bigger than old database, shove everything in it
if ( newPtr == NULL ) //Out of memory?
{
printf( "Error, couldn't create a new entry.\n" );
return; //Keeps gDatabase as old pointer
}
gDatabase = newPtr; //Copy all of newPtr back into gDatabase, make gDatabase main database again.
}
numDatabaseEntries += 1; //Increase database entries by 1;
//Allow user to input data and store it in the database
printf( "\nArtist name: " );
scanf( "%39s", gDatabase[ numDatabaseEntries - 1 ].artist );
fpurge( stdin );
printf( "Composer: " );
scanf( "%39s", gDatabase[ numDatabaseEntries - 1 ].composer );
fpurge( stdin );
printf( "Album name: " );
scanf( "%39s", gDatabase[ numDatabaseEntries - 1 ].albumName );
fpurge( stdin );
printf ("Track count: " );
scanf( "%d", &gDatabase[ numDatabaseEntries -1 ].trackCount );
fpurge( stdin );
printf( "Sampler? (y/n): " );
scanf( "%c", &yesOrNo );
fpurge( stdin );
gDatabase[ numDatabaseEntries - 1 ].isSampler = false; //Assigns it as false initially
gDatabase[ numDatabaseEntries - 1 ].isSampler = ( yesOrNo == 'y' || yesOrNo == 'Y' ); //Assigns it as true if Y or y is inputed
}
void List() //Function to list CDs
{
if ( gDatabase == NULL )
{
printf( "There are no CDs in the database!" );
return;
}
for ( int x = 0; x < numDatabaseEntries; ++x )
{
printf( "\nArtist name: %s\n", gDatabase[ x ].artist );
printf( "Composer: %s\n", gDatabase[ x ].composer );
printf( "Album name: %s\n", gDatabase[ x ].albumName );
printf( "No. of tracks: %d\n", gDatabase[ x ].trackCount );
if ( gDatabase[ x ].isSampler )
printf( "This CD is a sampler\n" );
}
}
void CleanUp() //Function to free memory
{
if ( gDatabase != NULL )
{
free( gDatabase );
gDatabase = NULL;
numDatabaseEntries = 0;
}
}
main.h:
Code:
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
struct DatabaseEntry;
//Defines custom data structure to use to store information on each CD
int numDatabaseEntries;
struct DatabaseEntry * gDatabase;
void NewEntry();
void List();
void CleanUp();
I think it's something to do with the data structure not being declared properly, I've tried doing different things with it but none of them have got rid of the error.
I apologise if I've done something completely wrong here, the lesson that I was following was a bit vague at times, and i wasn't that sure on where to put things in terms of header files etc.
Thanks.
Last edited by a moderator: