int main(int argc, char** argv) { int input; char *stockfile = NULL, *coinsfile = NULL; char charInput[MENUINPUT + EXTRA_SPACES]; tm_type tm; BOOLEAN quit = FALSE; /* check command line arguments */ if (argc != 3){ printf("Usage: tm <stock.csv> <coins.csv>\n"); exit(EXIT_FAILURE); } else{ /*Check if what the user has entered as arguments match stock.csv and coins.csv*/ if (strcmp(argv[1], FNAME1) && strcmp(argv[2], FNAME2) != 0){ printf("\nError: Cannot load stock.csv and coins.csv! Exiting...\n\n"); exit(EXIT_FAILURE); } } /* initialise data structures */ system_init(&tm); /* load data */ load_data(&tm, argv[1], argv[2]); /* test that everything has gone in initialisation and loading */ while(!quit) { /* display menu */ printf("\nMain Menu\n"); printf("1) Purchase Ticket\n"); printf("2) Display Tickets\n"); printf("3) Save and Exit\n"); printf("Administrator-Only Menu\n"); printf("4) Add Ticket\n"); printf("5) Remove Ticket\n"); printf("6) Display Coins\n"); printf("7) Restock Tickets\n"); printf("8) Restock Coins\n"); printf("9) Abort\n\n"); printf("Select your option (1-9): "); /* perform menu choice */ fgets(charInput, MENUINPUT + EXTRA_SPACES, stdin); /*Convert menu input to an int for use in switch statement*/ input = atoi(charInput); switch(input){ case 0: printf("Error: Not a valid option! Please try again.\n\n"); break; case 1: purchase_ticket(&tm); break; case 2: display_tickets(&tm); break; case 3: save_data(&tm, stockfile, coinsfile); system_free(&tm); exit(EXIT_SUCCESS); break; case 4: add_ticket(&tm); break; case 5: delete_ticket(&tm); break; case 6: display_coins(&tm); break; case 7: restock_tickets(&tm); break; case 8: restock_coins(&tm); break; case 9: quit = TRUE; break; } } /* free memory */ system_free(&tm); /* leave program */ exit(EXIT_SUCCESS); }
/** * manages the running of the program, initialises data structures, loads * data and handles the processing of options. The bulk of this function * should simply be calling other functions to get the job done. **/ int main(int argc, char **argv) { /* Represents the data structures to manage the system */ struct ppd_system system; struct menu_item menu[NUM_MENU_ITEMS]; char input[NUM_MENU_INPUT], *error; int option, i; BOOLEAN exit = FALSE; /* Validate command line arguments */ if ( argc != NUM_ARGS) { printf("Usage: ./playme <stock> <coins>\n\n"); return EXIT_FAILURE; } else { if(argv[ITEM_FILE_INDEX] == 0) { printf("stock_file failed!\n"); printf("Please make sure to input file name correctly.\n\n"); return EXIT_FAILURE; } if(argv[COIN_FILE_INDEX] == 0) { printf("coins_file failed!\n"); printf("Please make sure to input file name correctly.\n\n"); return EXIT_FAILURE; } } /* Init the system */ if(system_init(&system) != TRUE) { printf("System failed to initialise!\n"); system_free(&system); return EXIT_FAILURE; } /* Load data */ if(load_data(&system, argv[COIN_FILE_INDEX], argv[ITEM_FILE_INDEX]) != TRUE) { printf("Failed to load data!\n"); system_free(&system); return EXIT_FAILURE; } /* Test if everything has been initialised correctly */ #if 0 if(!display_items(&system) || !display_coins(&system)) abort_program(&system); #endif /* Initialise the menu system */ init_menu(menu); while(!exit) { /* Loop, asking for options from the menu */ for(i = 0; i < NUM_MENU_ITEMS; i++) { if(i == SELECT_DEFAULT) printf("\n\n== Default Selections ==\n========================\n"); else if(i == SELECT_ADMIN) printf("\n== Admin Selections ==\n========================\n"); printf("%d. %s\n", i + 1, menu[i].name); } while(!exit) { printf("\nPlease select what you would like to do: "); /* Get user input and assign to variable */ i = get_user_input(input, NUM_MENU_INPUT); /* Check for return to menu */ if(i == RTM) { printf("You have no where to return to!\n"); continue; } /* Check for invalid input */ if(i == FAILURE) { printf("Your text was too long!\n"); continue; } /* Convert given input to int and assign to option */ option = (int) strtol(input, &error, 0) - 1; /* Check if converted string inside menu range */ if(option >= 0 && option <= NUM_MENU_ITEMS) exit = TRUE; /* For all other values, echo output outside of range */ else printf("Input outside of range!\n"); } /* Reset exit BOOLEAN for part 2 */ exit = FALSE; /* Run each option selected */ if(menu[option].function(&system) != TRUE) printf("Option '%s' failed to complete!\n", menu[option].name); } return EXIT_SUCCESS; }