Hi there, i'm trying to make a program which translates a word from one language to another, using files (one file for each language)

i'm using fgets, to read a line from the file and then comparing it to what the user entered, and strcomp() to see if the words match, but for some reason, it doesn't work

Here is the code

#include <iostream>
#include <stdio.h>
#include <string>

using namespace std;
  
int main ()
{   int i=0;
    int bandera=0;
    int repite=0;
    char frase [100];
    char auxiliar [20];
    char auxiliar2[20];
      int opcion=0;
 
   
          do{
     
     FILE * pfile;
    FILE * pfile2;
    pfile = fopen ("quechua.txt" , "r");
    pfile2 = fopen ("espanol.txt" , "r");
            if (pfile == NULL) perror ("Error opening file");
             else if (pfile2 == NULL) perror ("Error opening file");
             else {
          cout<<"Digite palabra a traducir [en espanol]"<<endl;
          cin>>frase;
         
         while (!feof(pfile))
          {
          fgets (auxiliar,20,pfile2);
          fgets(auxiliar2,20,pfile);
          if (strcmp(frase,auxiliar)==0)
          {
            puts(auxiliar2);
            bandera=1;
         
          }
          }
          if (bandera==0)
          {
           cout<<"no se encontro la palabra buscada"<<endl;
           }
          fclose(pfile);
          fclose(pfile2);
          
          
          cout<<"Desea traducir otra cosa \n1-Si \n2-No"<<endl;
          cin>>repite;}
          }while (repite==1);
          
  
        
         
}

i would also like to know if there is a better way to accomplish this task, any help would be greatly appreciated :)

Recommended Answers

All 7 Replies

Oh, here are the files if they are of any help...

I really get tired of people simply saying "it doesn't work" and expecting us to figure out what it's doing wong. Tell us what doesn't work. What happens? Does is crash? Does it melt the monitor? Does it bomb South America?

If you need help -- give DETAILS

First of all put this

fclose(pfile); fclose(pfile2);

at the end ; I think that your program may work but only once , then

Does is crash? Does it melt the monitor?

You've used a good idea , searching the word in the espanol.txt then you showed the same line from quechua.txt . The program has to work .
Obs :
bandera never changes , for ex if it was once 1 , it will be 1 forever ==>

if (bandera==0)
          {
           cout<<"no se encontro la palabra buscada"<<endl;
           }

will not work .
Hope this helped

Traditionally a dictionary would be represented as a Map. Where the word would act as the key value into the map and the definition would correspond to the value for that key. Maybe you could produce a map for language conversion where the key value is a word in the original language and the value for that key is the translation.

So rather than reading you files line by line for each entered word you would read both files in their entirety while building your map and then each translation would be a search of the map for a key value.

This will require more memory than your current solution though.

Of course your solution seems to depend on there being a direct translation for each word in the file. What if there isn't? How are you going to handle situations where there isn't a one to one mapping of words and translations?

I really get tired of people simply saying "it doesn't work" and expecting us to figure out what it's doing wong...
If you need help -- give DETAILS

The problem is that the words never match, sorry for not saying that on the first place =\

Traditionally a dictionary would be represented as a Map. Where the word would act as the key value into the map and the definition would correspond to the value for that key. Maybe you could produce a map for language conversion where the key value is a word in the original language and the value for that key is the translation.

Unfortunately, i can't use maps for this exercise, it's either that or using lseek() which i think is a nore complicated way than using fgets ()

Of course your solution seems to depend on there being a direct translation for each word in the file. What if there isn't? How are you going to handle situations where there isn't a one to one mapping of words and translations?

There has to be a direct translation, the files are built that way (though one of the options is that the user can add words to the dictionary, but i guess some restriction at the time of the input would take charge of that)

Given that the fgets() is working fine, i assume strcmp() doesn't work for this program, but how else can i compare two char arrays? =S

again, thanks for your help :)

Have you looked at the actual documentation for fgets() ? Do you know what the function does with the \n of the line read?

Why can't you use a map? Is that a constraint of the assignment? Otherwise if you have to provide functionality for adding new words to the files then this becomes much more difficult as, although probably not 100% necessary, it would be good to add them in a sorted order. Keeping the dataset ordered provides you with a number of opportunities for optimising the searches through said dataset. If you have the entire dataset in memory then this is trivial and all you need to do is write the updated data on shutdown, where as maintaining a file on disk a line a time is going to present you with a number of problems.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.