1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
|
// THIS IS MAIN
#include <stdio.h> // get, fget, scanf, printf
#include <stdlib.h> // srand, rand, atof, NULL
#include "movie.h"
#include "mytime.h"
#include "dbllinkedlist.h"
#include "node.h"
int main (int argc, char* argv[]) {
dbl_linked_list_t list;
node_t* nodePtr;
movie_t movie;
FILE* inputFile;
int i, menuChoice = 0;
// Exit Program if there is no specified file.
if (argc < 2) {
printf ("Usage Error: No database specified. Contact your administrator.\n");
exit (EXIT_FAILURE);
}
// Open the File Stream
inputFile = fopen (argv[1], "rb");
// Exit Program if specified file does not exist.
if (inputFile == NULL) {
printf ("Movie Database Error. Contact your administrator. File %s does not exist. \n", argv[1]);
exit (EXIT_FAILURE);
}
// Get a character from the file stream, and put it back.
// Used to set EOF flag if needed.
int c = fgetc (inputFile);
ungetc (c, inputFile);
// Exit Program if the file is empty.
if (feof(inputFile)) {
printf ("Contact your administrator. File %s is empty.\n", argv[1]);
exit (EXIT_FAILURE);
}
// Initialize Dbl Linked List
createList (&list);
// Read in the first movie
fread (&movie, sizeof (movie_t), 1, inputFile);
// Read data for 1 movie, initialize node with that data, and insert node into list.
// Continue until EOF is reached.
while (!feof (inputFile)) {
// Initialize a node with data for 1 movie.
nodePtr = initNode (movie);
// Insert that node into the list.
insertNode (&list, nodePtr);
// Read data for the next movie.
fread (&movie, sizeof (movie_t), 1, inputFile);
}
fclose (inputFile);
while (menuChoice != 3) {
// Displays Menu to the user and prompts for option.
printf ("Welcome to Online Ticket System\n");
printf ("\t1) Show all movies\n\t2) Search for a movie\n\t3) Quit\nOption: ");
scanf ("%d", &menuChoice);
// Show all movies
if (menuChoice == 1) {
showAllMovies (&list);
}
// Search by movie title and display showings.
else if (menuChoice == 2) {
displayShowTimes (&list);
}
// Exit the Program
else if (menuChoice == 3) {
printf ("Thank you for using the program. Goodbye now.\n");
exit (EXIT_SUCCESS);
}
// If user does not enter 1, 2, or 3, displays Error.
else {
printf ("Menu Error. Invalid choice; please try again\n");
}
}
// Deallocates memory for all nodes linked in the list
deleteList (&list);
return 0;
}
|