/* inv.c */ #include #include #include #include "movie.h" #include "menu.h" /* prototypes */ void add( char *items, MOVIE_TYPE ***movies, int *nmovies ); void edit(); void list(); void delete(); /*---------------------------------------------------------- main() ----------------------------------------------------------*/ int main( int argc,char **argv ) { int more = 1; char choice; readMovieFile( "movies.dat" ); while ( more ) { choice = showMenu( "a movie","all the movies" ); switch ( choice ) { case 'a': add( "movies",&movies,&nmovies ); break; case 'e': edit(); break; case 'd': delete(); break; case 'l': list(); break; case 'q': more = 0; break; default: printf( "invalid choice. please try again. " ); } // end switch } // end while writeMovieFile( "movies.dat" ); } /* end of main() */ /*---------------------------------------------------------- add() this function is used to add a movie to the inventory. the function loops, adding more movies to the inventory until the user enters "quit". ----------------------------------------------------------*/ void add( char *items, MOVIE_TYPE ***movies, int *nmovies ) { MOVIE_TYPE *tmp = readMovie(); if ( tmp != NULL ) { *movies = (MOVIE_TYPE **)realloc( *movies,((*nmovies)+1)*sizeof( MOVIE_TYPE * )); if ( *movies != NULL ) { (*movies)[*nmovies] = tmp; (*nmovies)++; } else { printf( "error! can't add movies!\n" ); } } } /* end of add() */ /*---------------------------------------------------------- edit() this function is used to edit a movie in the inventory ----------------------------------------------------------*/ void edit() { char tmp[5]; int n, i; if ( nmovies > 0 ) { n = -1; while ( n < 0 ) { printf( "\nenter number of movie to edit: " ); scanf( "%d",&n ); fflush( stdin ); i = findNumber( n ); } strcpy( tmp,"y" ); while ( tolower( tmp[0] ) == 'y' ) { printf( "movie is: [%s]\n",movies[i]->name ); printf( "change it (y/n)? " ); scanf( "%s",tmp ); fflush( stdin ); if ( tolower( tmp[0] ) == 'y' ) { printf( "enter new movie: " ); movies[i]->name = fgets( movies[i]->name,MAX,stdin ); { char *p = strchr( movies[i]->name,'\n' ); *p = '\0'; } } } } } /* end of edit() */ /*---------------------------------------------------------- list() this function is used to list the movies in the inventory ----------------------------------------------------------*/ void list() { int i; if ( nmovies > 0 ) { printf( "\nhere are the movies in the inventory:\n" ); for ( i=0; i 0 ) { n = -1; while ( n < 0 ) { printf( "\nenter number of movie to delete: " ); scanf( "%d",&n ); fflush( stdin ); i = findNumber( n ); } for ( j=i; jnumber != number )) { i++; } if ( movies[i]->number == number ) { return i; } else { return -1; } } /* end of findNumber() */