I wrote a C language yesterda and found that there are some errors. However, I do not know how to fix them up.
Would appreciated if someone can help me.....THANKS!!!
--------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#define STR_MAX 254
#define NOUN 1
#define ADJECTIVE 2
#define VERB 3
typedef struct wordnode * wordnodePtr
typedef struct wordnode
{
char word[STR_MAX+1];
char meaning[STR_MAX+1];
wordnodePtr * next;
int dataType;
} WordNode;
typedef struct dictionary
{
wordnodePtr * head;
int wordCount;
int displayOption;
} Dictionary;
int main(void)
{
char string[STR_MAX+1];
int choice, exit;
Dictionary dict;
Init(dict);
exit = 0;
do{
printf("Search: ");
fgets(string, STR_MAX, stdin);
if( strcmp(string, "?") == 0)
{
printf("1. Add word\n"
"2. Delete word\n"
"3. Set display option\n"
"4. Exit display mode\n");
fgets(string, STR_MAX, stdin);
choice = atoi(string);
switch(choice)
{
case 1:
AddNewWord(dict);
break;
case 2:
DeleteWord(dict);
break;
case 3:
SetDisplayOption;
break;
case 4:
exit = 1;
break;
default:
break;
}
}else{
searchDictionary(dict, string);
}
} while( exit != 1);
}
Init(Dictionary * dict){
//Initialise dict linklist
dict->head = NULL
dict->wordCount = 0;
}
void AddToDictionary(Dictionary * dict, char * word, char * meaning, int dataType)
{
WordNode *newNode;
//add to linklist
newNode = ((WordNode *)malloc(sizeof(WordNode)));
if( newNode == NULL )
{
printf("Failed to allocate new memory space.");
}else{
strcpy(newNode->word, word);
strcpy(newNode->meaning, meaning);
newNode = dataType;
}
}
void SearchDictionary(Dictionary * dict, char * word)
{
int i, choice;
char string[STR_MAX+1];
char ** possibleList;
WordNode * current;
//search
i=0;
current = dict->head;
while( current != NULL )
{
if( strstr(word, current->word) == 0 )
{
if( dict->displayOption = current->dataType )
{
//print the possible list
printf("%d. %s\n", word, i++);
strcpy(possibleList[i], word);
}
}
current = current->next;
}
//select from the list
printf("\nEnter your choice: ");
fgets(string, STR_MAX, stdin);
choice = atoi(string);
if(possibleList[choice] == NULL)
{
printf("No such index");
return;
}
//display
current = dict->head;
while( current != NULL )
{
if( strcmp(possibleList[choice], current->word) == 0 )
{
printf("%s\n -----------------\n %s", word, meaning);
//done
return;
}
current = current->next;
}
}
int LoadDictionary(Dictionary * dict)
{
FILE * fp;
char * p;
char word[STR_MAX+1];
char meaning[STR_MAX+1];
char temp_str[STR_MAX+1];
char * filename = "dictionary.txt";
fp = fopen(filename, "r");
if( fp == NULL )
{
printf("Error");
return EXIT_FAILURE;
}
while(!feof(fp) && fgets(temp_str, STR_MAX, fp))
{
char word[STR_MAX+1], meaning[STR_MAX+1];
int dataType;
p = strtok(temp_str, ":");
strcpy(word, p);
p = strtok(NULL, ":");
dataType = p;
p = strtok(NULL, ":");
strcpy(meaning, p);
addToDictionary(dict, word, meaning, dataType);
}
}
void SaveToFile(Dictionary * dict)
{
char * filename = "dictionary.txt";
}
void AddNewWord(Dictionary * dict)
{
//User input
//scanf word, meaning, word type
//Call AddToDictionary(dict, word, meaning)
}
void DeletewWord(Dictionary * dict)
{
WordNode * current;
printf("Enter word to delete: ");
fgets(string, STR_MAX, stdin);
current = dict->head;
while( current != NULL )
{
if( strcmp(possibleList[choice], current->word) == 0 )
{
//found
printf("%s has been deleted\n", word;
free(current);
return;
}
current = current->next;
}
}
void SetDisplayOption(Dictionary * dict)
{
printf("Set Display Option:\n"
"1. Show All\n"
"2. Show Noun\n"
"3. Show Adjective\n"
"4. Show Verb");
fgets(string, STR_MAX, stdin);
dict->displayOption = atoi(string);
}