Link to home
Start Free TrialLog in
Avatar of cooldean
cooldean

asked on

English-German Dictionary

Hey guys,

I've been writing this program for days and I keep getting all these errors.  Basically, it is a English-German dictionary but it asks the user to lookup a word (l) in the dictionary (wordlist.txt), insert a word (i), delete a word (d), lookup a group of words with the same prefix (lg), and delete a group of words with the same prefix (dg).  The wordlist.txt is a file that is in my directory and i know the program won't run completely if you try running it.  However, I am getting errors that I don't know how to fix.  Can you please run it the code and see what's wrong?  

(This is a tough assignment for you guys..haha!)

Remember, assume that the wordlist.txt file is in the directory...
------------------------

#include <stdio.h>
#include <string.h>
#define MAXLEN 800

void extractSubstr(char str[], int from, int len, char substr[]) {
      int i;

      for (i = 0; i < len && str[from + i] != '\0'; i++) {
            substr[i] = str[from + i];
      }

      substr[i] = '\0';
}


void lookup() {
  int pos;
  int foundWord = 0;
  FILE *wordlist;

  string line, substr, s;
  wordlist = fopen("wordlist.txt","r");
  printf("Please enter a word to lookup: ");
  gets(s);

  fgets(line,MAXLEN,wordlist);

  while(line != EOF) {
    pos = strchr(line,'/');
    extractSubstr(line,0,pos,substr);
    if(strcmp(s,substr) == 0) {
      printf("Found %s.",s);
      extractSubstr(line,pos+1,strlen(line) - pos,substr);
      printf("German translation is %s\n",substr);
      return ;
    }
    else {
      fgets(line,MAXLEN,wordlist);
    }
  }
  printf("%s not found\n",s);
  fclose(wordlist);
}

void insert() {
  string eng, german;
  string temp1, temp2, temp2eng;
  int strcmpresult, pos, insertedWord;
  FILE *dictionary, *tempFile;

  printf("Enter an English word to insert: ");
  gets(eng);

  printf("Enter the German translation: ");
  gets(german);

  strcpy(temp1,eng);
  strcat(temp1,"/");
  strcat(temp1,german);
  wordlist = fopen("wordlist.txt","r");
  tempFile = fopen("temp.txt","w");

  fgets(temp2,MAXLEN,wordlist);
  pos = strchr(line, '/');
  extractSubstr(line,0,pos,temp2eng);
  insertedWord = 0;

  while(temp2 != EOF) {
    strcmpresult = strcmp(eng,temp2eng);
    if(strcmpresult <= 0) {
      fprintf(tempFile,"%s\n",temp2);
    }

    else if(strcmpresult > 0) {
      if(insertedWord == 0) {
      fprintf(tempFile,"%s\n",temp1);
      }
      fprintf(tempFile,"%s\n",temp2);
      insertedWord = 1;
    }
    fgets(temp2,MAXLEN,wordlist);
  }
  fclose(wordlist);
  fclose(tempFile);
  wordlist = fopen("wordlist.txt","w");
  tempFile = fopen("temp.txt","r");

  fgets(temp2,MAXLEN,tempFile);

  while(temp2 != EOF) {
    fprintf(temp2,"%s\n",wordlist);
  }
  fclose(wordlist);
  fclose(tempFile);
}

void delete() {
  string eng;
  string temp1, temp2, temp2eng;
  int strcmpresult, pos, insertedWord;
  FILE *wordlist, *tempFile;
 
  printf("Enter the word to delete: ");
  gets(eng);

  wordlist = fopen("wordlist.txt","r");
  tempFile = fopen("temp.txt","w");

  fgets(temp2,MAXLEN,wordlist);
  pos = strchr(line, '/');
  extractSubstr(line,0,pos,temp2eng);
  insertedWord = 0;

  while(temp2 != EOF) {
    strcmpresult = strcmp(eng,temp2eng);
    if(strcmpresult < 0) {
      fprintf(tempFile,"%s\n",temp2);
    }

    else if(strcmpresult > 0) {
      fprintf(tempFile,"%s\n",temp2);
      insertedWord = 1;
    }
    else {
      printf("Found the word, not writing to tempFile\n");
    }
    fgets(temp2,MAXLEN,wordlist);
  }

  fclose(wordlist);
  fclose(tempFile);
  wordlist = fopen("wordlist.txt","w");
  tempFile = fopen("temp.txt","r");

  fgets(temp2,MAXLEN,tempFile);

  while(temp2 != EOF) {
    fprintf(temp2,"%s\n",wordlist);
  }
  fclose(wordlist);
  fclose(tempFile);
}

void lookupGroup() {

  string engPrefix, temp;
  int found;
  FILE *wordlist;

  wordlist = fopen("wordList.txt");

  printf("Enter the prefix to look up a word group with that prefix:  ");
  gets(engPrefix);

  fgets(temp,MAXLEN,wordlist);
  found = 0;
  while(temp != EOF) {
    if(strncmp(temp,engPrefix,strlen(engPrefix)) == 0) {
      found = 1;
      printf("%s\n",temp);
    }
    fgets(temp, MAXLEN,wordlist);
  }
  if(found == 0) {
    printf("Not found\n");
  }
}

void deleteGroup() {
  string engPref;
  string temp1, temp2, temp2eng;
  int strcmpresult, pos, insertedWord;
  FILE *wordlist, *tempFile;

  printf("Enter the prefix to delete a word group with that prefix:  ");
  gets(engPref);

  wordlist = fopen("wordlist.txt","r");
  tempFile = fopen("temp.txt","w");

  fgets(temp2,MAXLEN,wordlist);
  insertedWord = 0;

  while(temp2 != EOF) {
    strcmpresult = strncmp(temp2,engPref,strlen(engPref));
    if(strcmpresult < 0) {
      fprintf(tempFile,"%s\n",temp2);
    }

    else if(strcmpresult > 0) {
      fprintf(tempFile,"%s\n",temp2);
      insertedWord = 1;
    }
    else {
      printf("Found the word, not writing to tempFile\n");
    }
    fgets(temp2,MAXLEN,wordlist);
  }

  fclose(wordlist);
  fclose(tempFile);
  wordlist = fopen("wordlist.txt","w");
  tempFile = fopen("temp.txt","r");

  fgets(temp2,MAXLEN,tempFile);

  while(temp2 != EOF) {
    fprintf(temp2,"%s\n",wordlist);
  }
  fclose(wordlist);
  fclose(tempFile);
}


main() {
  string userstring;

  printf("Welcome to CSE110 English-German Dictionary!\n"
           "Please make a selection:\n"
             "1) l to lookup a word\n"
             "2) i to insert a word\n"
             "3) d to delete a word\n"
             "4) lg to look up a group of words with a prefix\n"
             "5) dg to delete a group of words with a prefix\n"
             "6) q to quit the program\n");

  while(1) {

    gets(userstring);

    if(strcmp(userstring,"l") == 0) {
      lookup();
    }
    else if(strcmp(userstring,"i") == 0) {
      insert();
    }
      else if(strcmp(userstring,"d") == 0) {
      delete();
    }
      else if(strcmp(userstring,"lg") == 0) {
      lookupGroup();
    }
      else if(strcmp(userstring,"dg") == 0) {
      deleteGroup();
    }
    else if(strcmp(userstring,"q") == 0) {
      return 0;
    }
    else {
      printf("unknown user command\n");
    }
  }
}

Avatar of sunnycoder
sunnycoder
Flag of India image

your program is a complete mess as far as types are concerned ... you should be careful about data types

1. string line, substr, s;
string is not a data type in C ... it has to be a char * or a char array

2. gets(s);
gets is not to be used

3. fgets(line,MAXLEN,wordlist);

 while(line != EOF) {
line is a char * and you are comparing it with an int !!!
while ( fgets (...) != NULL )
is perhaps what you want

the list just goes on and on ...
these errors are neither hard to recognize nor hard to remove ... I would suggest that you take up this as a debugging assignment ... though I can clean the code in less than 10 minutes, I would prefer to make you do it for your own good
Avatar of cooldean
cooldean

ASKER

I fixed it up...it has no more errors..but it won't display the string for each selection...it seems like it won't go through loops...

Man, I've spent hours already on this...it sucks..


----------------
#include <stdio.h>
#include <string.h>
#define MAXLEN 80



void extractSubstr(char str[], int from, int len, char substr[]) {
      int i;

      for (i = 0; i < len && str[from + i] != '\0'; i++) {
            substr[i] = str[from + i];
      }

      substr[i] = '\0';
}

/* Look Up Function */

void lookup() {
  int pos;
  int foundWord = 0;
  FILE *wordlist;

  char line[80], substr[80], s[80];
  wordlist = fopen("wordlist.txt","r");
  printf("Please enter a word to lookup: ");
  gets(s);

  fgets(line,MAXLEN,wordlist);

  while(line != EOF) {
    pos = strchr(line,'/');
    extractSubstr(line,0,pos,substr);
    if(strcmp(s,substr) == 0) {
      printf("Found %s",s);
      extractSubstr(line,pos+1,strlen(line) - pos,substr);
      printf("German translation is %s\n",substr);
      return ;
    }
    else {
      fgets(line,MAXLEN,wordlist);
    }
  }
  printf("%s not found\n",s);
  fclose(wordlist);
}

/* Insert Function */

void insert() {
  char eng[80], german[80], line[80];
  char temp1[80], temp2[80], temp2eng[80];
  int strcmpresult, pos, insertedWord, wordlist;
  FILE *dictionary, *tempFile;

  printf("Enter an English word to insert: ");
  gets(eng);

  printf("Enter the German translation: ");
  gets(german);

  strcpy(temp1,eng);
  strcat(temp1,"/");
  strcat(temp1,german);
  wordlist = fopen("wordlist.txt","r");
  tempFile = fopen("temp.txt","w");

  fgets(temp2,MAXLEN,wordlist);
  pos = strchr(line, '/');
  extractSubstr(line,0,pos,temp2eng);
  insertedWord = 0;

  while(temp2 != EOF) {
    strcmpresult = strcmp(eng,temp2eng);
    if(strcmpresult <= 0) {
      fprintf(tempFile,"%s\n",temp2);
    }

    else if(strcmpresult > 0) {
      if(insertedWord == 0) {
      fprintf(tempFile,"%s\n",temp1);
      }
      fprintf(tempFile,"%s\n",temp2);
      insertedWord = 1;
      printf("Done!\n");
      break;
    }

    fgets(temp2,MAXLEN,wordlist);

  }

  fclose(wordlist);
  fclose(tempFile);
  wordlist = fopen("wordlist.txt","w");
  tempFile = fopen("temp.txt","r");

  fgets(temp2,MAXLEN,tempFile);

  while(temp2 != EOF) {
    printf(temp2,"%s\n",wordlist);
      break;
  }

  fclose(wordlist);
  fclose(tempFile);
}

/* Delete Function */

void delete() {
  char eng[80], line[80];
  char temp1[80], temp2[80], temp2eng[80];
  int strcmpresult, pos, insertedWord;
  FILE *wordlist, *tempFile;
 
  printf("Enter the word to delete: ");
  gets(eng);

  wordlist = fopen("wordlist.txt","r");
  tempFile = fopen("temp.txt","w");

  fgets(temp2,MAXLEN,wordlist);
  pos = strchr(line, '/');
  extractSubstr(line,0,pos,temp2eng);
  insertedWord = 0;

  while(temp2 != EOF) {
    strcmpresult = strcmp(eng,temp2eng);
    if(strcmpresult < 0) {
      fprintf(tempFile,"%s\n",temp2);
    }

    else if(strcmpresult > 0) {
      printf(tempFile,"%s\n",temp2);
      insertedWord = 1;
    }
      
    else {
      printf("Found the word, not writing to tempFile\n");
    }

    fgets(temp2,MAXLEN,wordlist);
  }


  fclose(wordlist);
  fclose(tempFile);
  wordlist = fopen("wordlist.txt","w");
  tempFile = fopen("temp.txt","r");

  fgets(temp2,MAXLEN,tempFile);

  while(temp2 != EOF) {
    printf(temp2,"%s\n",wordlist);
  }
  fclose(wordlist);
  fclose(tempFile);
}

/* Lookup Group Function */

void lookupGroup() {

  char engPrefix[80], temp[80];
  int found;
  FILE *wordlist;

  wordlist = fopen("wordList.txt","r");

  printf("Enter the prefix to look up a word group with that prefix:  ");
  gets(engPrefix);

  fgets(temp,MAXLEN,wordlist);
  found = 0;
  while(temp != EOF) {
    if(strncmp(temp,engPrefix,strlen(engPrefix)) == 0) {
      found = 1;
      printf("%s\n",temp);
    }
    fgets(temp, MAXLEN,wordlist);
  }
  if(found == 0) {
    printf("Not found\n");
  }
}


/* Delete Group Function */

void deleteGroup() {
  char engPref[80];
  char temp1[80], temp2[80], temp2eng[80];
  int strcmpresult, pos, insertedWord;
  FILE *wordlist, *tempFile;

  printf("Enter the prefix to delete a word group with that prefix:  ");
  gets(engPref);

  wordlist = fopen("wordlist.txt","r");
  tempFile = fopen("temp.txt","w");

  fgets(temp2,MAXLEN,wordlist);
  insertedWord = 0;

  while(temp2 != EOF) {
    strcmpresult = strncmp(temp2,engPref,strlen(engPref));
    if(strcmpresult < 0) {
      fprintf(tempFile,"%s\n",temp2);
    }

    else if(strcmpresult > 0) {
      fprintf(tempFile,"%s\n",temp2);
      insertedWord = 1;
    }
    else {
      printf("Found the word, not writing to tempFile\n");
    }
    fgets(temp2,MAXLEN,wordlist);
  }

  fclose(wordlist);
  fclose(tempFile);
  wordlist = fopen("wordlist.txt","w");
  tempFile = fopen("temp.txt","r");

  fgets(temp2,MAXLEN,tempFile);

  while(temp2 != EOF) {
    fprintf(temp2,"%s\n",wordlist);
  }
  fclose(wordlist);
  fclose(tempFile);
}

main() {
  char userstring[80];

  printf("Welcome to CSE110 English-German Dictionary!\n");

  while(1) {

  printf("Please make a selection:\n"
             "1) l to lookup a word\n"
             "2) i to insert a word\n"
             "3) d to delete a word\n"
             "4) lg to look up a group of words with a prefix\n"
             "5) dg to delete a group of words with a prefix\n"
             "6) q to quit the program\n");

    gets(userstring);

    if(strcmp(userstring,"l") == 0) {
      lookup();
    }
    else if(strcmp(userstring,"i") == 0) {
      insert();

    }
      else if(strcmp(userstring,"d") == 0) {
      delete();
    }
      else if(strcmp(userstring,"lg") == 0) {
      lookupGroup();
    }
      else if(strcmp(userstring,"dg") == 0) {
      deleteGroup();
    }
    else if(strcmp(userstring,"q") == 0) {
      return 0;
    }
    else {
      printf("unknown user command\n");
    }
  }
}

fgets(line,MAXLEN,wordlist);

 while(line != EOF) {
   pos = strchr(line,'/');
   extractSubstr(line,0,pos,substr);
   if(strcmp(s,substr) == 0) {
     printf("Found %s",s);
     extractSubstr(line,pos+1,strlen(line) - pos,substr);
     printf("German translation is %s\n",substr);
     return ;
   }
   else {
     fgets(line,MAXLEN,wordlist);
   }
 }

you need to reorganize the loop ...

while ( fgets (line,MAXLEN,wordlist) != NULL )
{
        ...
}

more over, it seems that all errors are not yet fixed ... do not ignore the warnings
test64.c:31: warning: comparison between pointer and integer
test64.c:32: warning: assignment makes integer from pointer without a cast
test64.c: In function `insert':
test64.c:65: warning: assignment makes integer from pointer without a cast
test64.c:68: warning: passing arg 3 of `fgets' makes pointer from integer without a cast
test64.c:69: warning: assignment makes integer from pointer without a cast
test64.c:73: warning: comparison between pointer and integer
test64.c:89: warning: passing arg 3 of `fgets' makes pointer from integer without a cast
test64.c:93: warning: passing arg 1 of `fclose' makes pointer from integer without a cast
test64.c:95: warning: assignment makes integer from pointer without a cast
test64.c:100: warning: comparison between pointer and integer
test64.c:105: warning: passing arg 1 of `fclose' makes pointer from integer without a cast
test64.c: In function `delete':
test64.c:124: warning: assignment makes integer from pointer without a cast
test64.c:128: warning: comparison between pointer and integer
test64.c:135: warning: passing arg 1 of `printf' from incompatible pointer type
test64.c:154: warning: comparison between pointer and integer
test64.c: In function `lookupGroup':
test64.c:176: warning: comparison between pointer and integer
test64.c: In function `deleteGroup':
test64.c:206: warning: comparison between pointer and integer
test64.c:229: warning: comparison between pointer and integer
test64.c:230: warning: passing arg 1 of `fprintf' from incompatible pointer type

these are all indications of an imminent error unless you are perfectly aware of what you are upto
ok..ive eliminated the errors...but it still won't display!  The revised code is below...

-----------------

#include <stdio.h>
#include <string.h>
#define MAXLEN 80


void extractSubstr(char str[], int from, int len, char substr[]) {
      int i;

      for (i = 0; i < len && str[from + i] != '\0'; i++) {
            substr[i] = str[from + i];
      }

      substr[i] = '\0';
}

/* Look Up Function */

void lookup() {
  int pos;
  int foundWord = 0;
  FILE *wordlist;

  char line[80], substr[80], s[80];
  wordlist = fopen("wordlist.txt","r");
  printf("Please enter a word to lookup: ");
  gets(s);



  while (fgets (line,MAXLEN,wordlist) != NULL ) {
    pos = strchr(line,'/') - line;
    extractSubstr(line,0,pos,substr);
    if(strcmp(s,substr) == 0) {
      printf("Found %s",s);
      extractSubstr(line,pos+1,strlen(line) - pos,substr);
      printf("German translation is %s\n",substr);
      return ;
    }
    else {
      fgets(line,MAXLEN,wordlist);
    }
  }
  printf("%s not found\n",s);
  fclose(wordlist);
}

/* Insert Function */

void insert() {
  char eng[80], german[80], line[80];
  char temp1[80], temp2[80], temp2eng[80];
  int strcmpresult, pos, insertedWord;
  FILE *wordlist, *tempFile;

  printf("Enter an English word to insert: ");
  gets(eng);

  printf("Enter the German translation: ");
  gets(german);

  strcpy(temp1,eng);
  strcat(temp1,"/");
  strcat(temp1,german);
  wordlist = fopen("wordlist.txt","r");
  tempFile = fopen("temp.txt","w");

  fgets(temp2,MAXLEN,wordlist);
  pos = strchr(line, '/') - line;
  extractSubstr(line,0,pos,temp2eng);
  insertedWord = 0;

  while(temp2 != NULL) {
    strcmpresult = strcmp(eng,temp2eng);
    if(strcmpresult <= 0) {
      fprintf(tempFile,"%s\n",temp2);
    }

    else if(strcmpresult > 0) {
      if(insertedWord == 0) {
      fprintf(tempFile,"%s\n",temp1);
      }
      fprintf(tempFile,"%s\n",temp2);
      insertedWord = 1;
      printf("Done!\n");
      break;
    }

    fgets(temp2,MAXLEN,wordlist);

  }

  fclose(wordlist);
  fclose(tempFile);
  wordlist = fopen("wordlist.txt","w");
  tempFile = fopen("temp.txt","r");

  fgets(temp2,MAXLEN,tempFile);

  while(temp2 != NULL) {
    printf(temp2,"%s\n",wordlist);
      break;
  }

  fclose(wordlist);
  fclose(tempFile);
}

/* Delete Function */

void delete() {
  char eng[80], line[80];
  char temp2[80], temp2eng[80];
  int strcmpresult, pos, insertedWord;
  FILE *wordlist, *tempFile;
 
  printf("Enter the word to delete: ");
  gets(eng);

  wordlist = fopen("wordlist.txt","r");
  tempFile = fopen("temp.txt","w");

  fgets(temp2,MAXLEN,wordlist);
  pos = strchr(line, '/') - line;
  extractSubstr(line,0,pos,temp2eng);
  insertedWord = 0;

  while(temp2 != NULL) {
    strcmpresult = strcmp(eng,temp2eng);
    if(strcmpresult < 0) {
      fprintf(tempFile,"%s\n",temp2);
    }

    else if(strcmpresult > 0) {
      fprintf(tempFile,"%s\n",temp2);
      insertedWord = 1;
    }
      
    else {
      printf("Found the word, not writing to tempFile\n");
    }

    fgets(temp2,MAXLEN,wordlist);
  }


  fclose(wordlist);
  fclose(tempFile);
  wordlist = fopen("wordlist.txt","w");
  tempFile = fopen("temp.txt","r");

  fgets(temp2,MAXLEN,tempFile);

  while(temp2 != NULL) {
    printf(temp2,"%s\n",wordlist);
  }
  fclose(wordlist);
  fclose(tempFile);
}

/* Lookup Group Function */

void lookupGroup() {

  char engPrefix[80], temp[80];
  int found;
  FILE *wordlist;

  wordlist = fopen("wordList.txt","r");

  printf("Enter the prefix to look up a word group with that prefix:  ");
  gets(engPrefix);

  fgets(temp,MAXLEN,wordlist);
  found = 0;
  while(temp != NULL) {
    if(strncmp(temp,engPrefix,strlen(engPrefix)) == 0) {
      found = 1;
      printf("%s\n",temp);
    }
    fgets(temp, MAXLEN,wordlist);
  }
  if(found == 0) {
    printf("Not found\n");
  }
}


/* Delete Group Function */

void deleteGroup() {
  char engPref[80];
  char temp2[80], temp2eng[80];
  int strcmpresult, pos, insertedWord;
  FILE *wordlist, *tempFile;

  printf("Enter the prefix to delete a word group with that prefix:  ");
  gets(engPref);

  wordlist = fopen("wordlist.txt","r");
  tempFile = fopen("temp.txt","w");

  fgets(temp2,MAXLEN,wordlist);
  insertedWord = 0;

  while(temp2 != NULL) {
    strcmpresult = strncmp(temp2,engPref,strlen(engPref));
    if(strcmpresult < 0) {
      fprintf(tempFile,"%s\n",temp2);
    }

    else if(strcmpresult > 0) {
      fprintf(tempFile,"%s\n",temp2);
      insertedWord = 1;
    }
    else {
      printf("Found the word, not writing to tempFile\n");
    }
    fgets(temp2,MAXLEN,wordlist);
  }

  fclose(wordlist);
  fclose(tempFile);
  wordlist = fopen("wordlist.txt","w");
  tempFile = fopen("temp.txt","r");

  fgets(temp2,MAXLEN,tempFile);

  while(temp2 != NULL) {
    fprintf(wordlist, "%s\n",temp2);
  }
  fclose(wordlist);
  fclose(tempFile);
}

main() {
  char userstring[80];

  printf("Welcome to CSE110 English-German Dictionary!\n");

  while(1) {

  printf("Please make a selection:\n"
             "1) l to lookup a word\n"
             "2) i to insert a word\n"
             "3) d to delete a word\n"
             "4) lg to look up a group of words with a prefix\n"
             "5) dg to delete a group of words with a prefix\n"
             "6) q to quit the program\n");

    gets(userstring);

    if(strcmp(userstring,"l") == 0) {
      lookup();
    }
    else if(strcmp(userstring,"i") == 0) {
      insert();
    }
      else if(strcmp(userstring,"d") == 0) {
      delete();
    }
      else if(strcmp(userstring,"lg") == 0) {
      lookupGroup();
    }
      else if(strcmp(userstring,"dg") == 0) {
      deleteGroup();
    }
    else if(strcmp(userstring,"q") == 0) {
      return 0;
    }
    else {
      printf("unknown user command\n");
    }
  }
}

>while(temp2 != NULL) {
>   strcmpresult = strcmp(eng,temp2eng);
>   if(strcmpresult <= 0) {
>     fprintf(tempFile,"%s\n",temp2);
>   }

you are not changing the value of temp anywhere in the loop ... this is an infinite loop...

you are having several infinte loops of the same kind everywhere in your program
I fixed it...but it only works for insert.  I can't get the other selections to work...Please help.

--------------------------------
#include <stdio.h>
#include <string.h>
#define MAXLEN 80


void extractSubstr(char str[], int from, int len, char substr[]) {
      int i;

      for (i = 0; i < len && str[from + i] != '\0'; i++) {
            substr[i] = str[from + i];
      }

      substr[i] = '\0';
}

/* Look Up Function */

void lookup() {
  int pos;
  int foundWord = 0;
  FILE *wordlist;

  char line[80], substr[80], s[80];
  wordlist = fopen("wordlist.txt","r");
  printf("Please enter a word to lookup: ");
  gets(s);



  while (fgets (line,MAXLEN,wordlist) != NULL ) {
    pos = strchr(line,'/') - line;
    extractSubstr(line,0,pos,substr);
    if(strcmp(s,substr) == 0) {
      printf("Found %s",s);
      extractSubstr(line,pos+1,strlen(line) - pos,substr);
      printf("German translation is %s\n",substr);
      return ;
    }
    else {
      fgets(line,MAXLEN,wordlist);
    }
  }
  printf("%s not found\n",s);
  fclose(wordlist);
}

/* Insert Function */

void insert() {
  char eng[80], german[80], line[80];
  char temp1[80], temp2[80], temp2eng[80];
  int strcmpresult, pos, insertedWord;
  FILE *wordlist, *tempFile;
  strcmpresult = 0;
  printf("Enter an English word to insert: ");
  gets(eng);

  printf("Enter the German translation: ");
  gets(german);

  strcpy(temp1,eng);
  strcat(temp1,"/");
  strcat(temp1,german);  //Temp1 is the word

  printf("Inserting %s into dictionary\n",temp1);
  wordlist = fopen("wordlist.txt","r");
  tempFile = fopen("temp.txt","w");


  insertedWord = 0;
  fgets(temp2,MAXLEN,wordlist);
  pos = strchr(temp2, '/') - temp2;
  extractSubstr(temp2,0,pos,temp2eng);
  insertedWord = 0;

  while(strncmp(temp2,"zero/null",9) != 0) {
    strcmpresult = strcmp(eng,temp2eng);

    if(strcmpresult > 0) {
      fprintf(tempFile,"%s",temp2);
    }

    else if(strcmpresult <= 0) {
      if(insertedWord == 0) {
      fprintf(tempFile,"%s\n",temp1);
      printf("Inserted!\n");
      }
      fprintf(tempFile,"%s",temp2);
      insertedWord = 1;
    }

    fgets(temp2,MAXLEN,wordlist);
    pos = strchr(temp2, '/') - temp2;
    extractSubstr(temp2,0,pos,temp2eng);
  }
  fprintf(tempFile,"zero/null");
  fclose(wordlist);
  fclose(tempFile);
  //tempFile is correct, now dump everything in reverse
  wordlist = fopen("wordlist.txt","w");
  tempFile = fopen("temp.txt","r");

  fgets(temp2,MAXLEN,tempFile);

  while(strncmp(temp2,"zero/null",9) != 0) {
    fprintf(wordlist,"%s",temp2);
    fgets(temp2,MAXLEN,tempFile);
  }

  fclose(wordlist);
  fclose(tempFile);
}

/* Delete Function */

void delete() {
  char eng[80], line[80];
  char temp1[80], temp2[80], temp2eng[80];
  int strcmpresult, pos, insertedWord;
  FILE *wordlist, *tempFile;
 
  printf("Enter the word to delete: ");
  gets(eng);

  wordlist = fopen("wordlist.txt","r");
  tempFile = fopen("temp.txt","w");

  printf("Deleting %s",eng);
  fgets(temp2,MAXLEN,wordlist);
  pos = strchr(line, '/') - line;
  extractSubstr(line,0,pos,temp2eng);
  insertedWord = 0;

  while(temp2 != NULL) {
    strcmpresult = strcmp(eng,temp2eng);
    if(strcmpresult < 0) {
      fprintf(tempFile,"%s\n",temp2);
    }

    else if(strcmpresult > 0) {
      fprintf(tempFile,"%s\n",temp2);
      insertedWord = 1;
    }
      
    else {
      printf("Found the word, not writing to tempFile\n");
    }

    fgets(temp2,MAXLEN,wordlist);
  }


  fclose(wordlist);
  fclose(tempFile);
  wordlist = fopen("wordlist.txt","w");
  tempFile = fopen("temp.txt","r");

  fgets(temp2,MAXLEN,tempFile);

  while(temp2 != NULL) {
    printf(temp2,"%s\n",wordlist);
  }
  fclose(wordlist);
  fclose(tempFile);
}

/* Lookup Group Function */

void lookupGroup() {

  char engPrefix[80], temp[80];
  int found;
  FILE *wordlist;

  wordlist = fopen("wordList.txt","r");

  printf("Enter the prefix to look up a word group with that prefix:  ");
  gets(engPrefix);

  fgets(temp,MAXLEN,wordlist);
  found = 0;
  while(temp != NULL) {
    if(strncmp(temp,engPrefix,strlen(engPrefix)) == 0) {
      found = 1;
      printf("%s\n",temp);
    }
    fgets(temp, MAXLEN,wordlist);
  }
  if(found == 0) {
    printf("Not found\n");
  }
}


/* Delete Group Function */

void deleteGroup() {
  char engPref[80];
  char temp1[80], temp2[80], temp2eng[80];
  int strcmpresult, pos, insertedWord;
  FILE *wordlist, *tempFile;

  printf("Enter the prefix to delete a word group with that prefix:  ");
  gets(engPref);

  wordlist = fopen("wordlist.txt","r");
  tempFile = fopen("temp.txt","w");

  fgets(temp2,MAXLEN,wordlist);
  insertedWord = 0;

  while(temp2 != NULL) {
    strcmpresult = strncmp(temp2,engPref,strlen(engPref));
    if(strcmpresult < 0) {
      fprintf(tempFile,"%s\n",temp2);
    }

    else if(strcmpresult > 0) {
      fprintf(tempFile,"%s\n",temp2);
      insertedWord = 1;
    }
    else {
      printf("Found the word, not writing to tempFile\n");
    }
    fgets(temp2,MAXLEN,wordlist);
  }

  fclose(wordlist);
  fclose(tempFile);
  wordlist = fopen("wordlist.txt","w");
  tempFile = fopen("temp.txt","r");

  fgets(temp2,MAXLEN,tempFile);

  while(temp2 != NULL) {
    fprintf(wordlist, "%s\n",temp2);
  }
  fclose(wordlist);
  fclose(tempFile);
}

main() {
  char userstring[80];

  printf("Welcome to CSE110 English-German Dictionary!\n");

  while(1) {

  printf("Please make a selection:\n"
             "1) l to lookup a word\n"
             "2) i to insert a word\n"
             "3) d to delete a word\n"
             "4) lg to look up a group of words with a prefix\n"
             "5) dg to delete a group of words with a prefix\n"
             "6) q to quit the program\n");

    gets(userstring);

    if(strcmp(userstring,"l") == 0) {
      lookup();
    }
    else if(strcmp(userstring,"i") == 0) {
      insert();
    }
      else if(strcmp(userstring,"d") == 0) {
      delete();
    }
      else if(strcmp(userstring,"lg") == 0) {
      lookupGroup();
    }
      else if(strcmp(userstring,"dg") == 0) {
      deleteGroup();
    }
    else if(strcmp(userstring,"q") == 0) {
      return 0;
    }
    else {
      printf("unknown user command\n");
    }
  }
}

ASKER CERTIFIED SOLUTION
Avatar of sunnycoder
sunnycoder
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial