/* * Eliasz Zurawka * Jan 29th 2007 * Secure Programming Assignment #2 */ #include #include #include #include #define SENTENCE_MAX 3 #define ENTRY_MAX 50 /* The following typedefs are just used to simplify how we refer to the structure. Just think of typedefs as aliases. The first typedef replaces the declaration struct dict_entry with dict_entry. The second makes it simpler to refer to a dict_entry pointer */ typedef struct dict_entry dict_entry; typedef dict_entry *dict_entryp; struct dict_entry { char keyword[80]; char sentence[SENTENCE_MAX][80]; } dictionary[ENTRY_MAX]; int entry_count = 0; /* ******************************************** FUNCTION NAME: get_definition PURPOSE: Uses gets to receive input of a definition from the user INPUT: None OUTPUT: A dict_entry structure AUTHOR: EZ *********************************************** */ dict_entry get_definition() { fflush(stdin); dict_entry *entP = NULL; entP = (struct dict_entry *) malloc(sizeof(struct dict_entry)); //gets(entP->keyword);//If this is not here it will a \n from somewhere // even though fflush is called above...very odd. printf("Enter Keyword: "); fgets(entP->keyword, 80, stdin); printf("Enter Line 1: "); fgets(entP->sentence[0], 80, stdin); printf("Enter Line 2: "); fgets(entP->sentence[1], 80, stdin); printf("Enter Line 3: "); fgets(entP->sentence[2], 80, stdin); return *entP; } /* *********************************************** FUNCTION NAME: display_entry PURPOSE: Prints to stdout the members of the dictionary structure INPUT: display_rec - a pointer to a dict_entry structure OUTPUT: None AUTHOR: EZ ************************************************* */ void display_entry(dict_entryp display_rec) { printf("Keyword:\t%s", display_rec->keyword); printf("\tDef1:\t%s", display_rec->sentence[0]); printf("\tDef2:\t%s", display_rec->sentence[1]); printf("\tDef3:\t%s", display_rec->sentence[2]); } /* *********************************************** FUNCTION NAME: list_entries PURPOSE: Prints all the dictionary entries currently in the dictionary INPUT: None OUTPUT: None AUTHOR: EZ ************************************************* */ void list_entries() { int i = 0; for(i = 0; i < entry_count; i++) { display_entry(&dictionary[i]); } } /* *********************************************** FUNCTION NAME: find_entry PURPOSE: Locates an entry in the dictionary array of structures that matches the keyword passed in INPUT: Keyword - a character string OUTPUT: An integer which is the index of the matching entry in the array of dictionary structures, -1 if not found AUTHOR: EZ ************************************************* */ int find_entry(char *keyword) { int count= 0; while(count <= entry_count && strcmp(dictionary[count].keyword, keyword) != 0) count++; if(count == entry_count) { return -1; } return count; } /* *********************************************** FUNCTION NAME: callFind PURPOSE: Present to keep the main method clean. Initializes the find and displays it. INPUT: None OUTPUT: None AUTHOR: EZ ************************************************* */ void callFind() { char searchWord[80]; printf("Enter a keyword to find: "); //scanf("%s", &searchWord); fgets(searchWord, 80, stdin); printf("Searching for %s\n", searchWord); int pos = find_entry(searchWord); if(pos >= 0) { display_entry(&dictionary[pos]); } else { printf("Could not find item."); } } /* *********************************************** FUNCTION NAME: main for tiny_dict.c PURPOSE: Present a menu of the available functions, retrieve the response from the user and call the appropriate function INPUT: None OUTPUT: 0 if error, 1 if safe exit AUTHOR: EZ ************************************************* */ int main() { int done = 0; int choice = 0; char choiceString[20]; while (!done) { int pos = 0; printf("<<<< Your Webster >>>>\n"); printf("1) Enter a definition.\n"); printf("2) List all entries.\n"); printf("3) Display an entry.\n"); printf("4) Exit.\n"); printf("Choice: "); //Needed to change these lines, was giving weird results //scanf("%s", choiceString); fgets(choiceString, 20, stdin); choice = atoi(choiceString); fflush(stdin); /* This flushes \n out of the buffer */ switch (choice) { case 1: /* get_definition() will be invoked here */ //printf("Get Def"); if(entry_count >= 79) { printf("Dictionary Full.\n"); } else { dict_entry newWord = get_definition(); dictionary[entry_count++] = newWord; fflush(stdin); } break; case 2: /* list_entries() will be invoked here */ list_entries(); break; case 3: /* display_entry() will be invoked here */ callFind(); break; case 4: done = 1; break; default: printf("\nInvalid choice: try again. \n"); choice = 0; } } }