menu_options current_option; cdc_entry current_cdc_entry; int command_result; memset(&current_cdc_entry, '\0', sizeof(current_cdc_entry));if (argc >1) { command_result = command_mode(argc, argv); exit(command_result); } announce(); if (!database_initialize(0)) { fprintf(stderr, "Sorry, unable to initialize database\n"); fprintf(stderr, "To create a new database use %s -i\n", argv[0]); exit(EXIT_FAILURE); }5. Теперь вы готовы обрабатывать ввод пользователя. Вы остаетесь в цикле, запрашивая пункт меню и обрабатывая его до тех пор, пока пользователь не выберет выход. Обратите вниманий на то, что вы передаете структуру
current_cdc_entryshow_menu while (current_option != mo_exit) { current_option = show_menu(&current_cdc_entry); switch(current_option) { case mo_add_cat: if (enter_new_cat_entry(&current_cdc_entry)) { if (!add_cdc_entry(current_cdc_entry)) { fprintf(stderr, "Failed to add new entry\n"); memset(&current_cdc_entry, '\0', sizeof(current_cdc_entry)); } } break; case mo_add_tracks: enter_new_track_entries(&current_cdc_entry); break; case mo_del_cat: del_cat_entry(&current_cdc_entry); break; case mo_find_cat: current_cdc_entry = find_cat(); break; case mo_list_cat_tracks: list_tracks(&current_cdc_entry); break; case mo_del_tracks: del_track_entries(&current_cdc_entry); break; case mo_count_entries: count_all_entries(); break; case mo_exit: break; case mo_invalid: break; default: break; } /* switch */ } /* while */ 6. Когда цикл в функции
mainannounce database_close(); exit(EXIT_SUCCESS);} /* main */static void announce(void) { printf("\n\nWelcome to the demonstration CD catalog database \ program\n");}7. Здесь вы реализуете функцию
show_menustatic menu_options show_menu(const cdc_entry *cdc_selected) { char tmp_str[TMP_STRING_LEN + 1]; menu_options option_chosen = mo_invalid; while (option_chosen == mo_invalid) { if (cdc_selected->catalog[0]) { printf("\n\nCurrent entry: "); printf("%s, %s, %a, %s\n", cdc_selected->catalog, cdc_selected->title, cdc_selected->type, cdc_selected->artist); printf("\n"); printf("1 - add new CD\n"); printf("2 — search for a CD\n"); printf("3 — count the CDs and tracks in the database\n"); printf("4 — re-enter tracks for current CD\n"); printf("5 - delete this CD, and all its tracks\n"); printf("6 - list tracks for this CD\n"); printf("q — quit\n"); printf("\nOption: "); fgets(tmp_str, TMP_STRING_LEN, stdin); switch(tmp_str[0]) { case '1': option_chosen = mo_add_cat; break; case '2': option_chosen = mo_find_cat; break; case '3': option_chosen = mo_count_entries; break; case '4': option_chosen = mo_add_tracks;