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 :)

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

Attachments
anciano
milagro
pretexto
estornudar
estornudo
llevar
ejemplo
escoger
vestido
garrapata
aguja
estircol
bao
escarabajo
defecar
cavar
bien
arreglar
restaurar
reconciliarse
perro
descontrolarse
escarnecer
practicar
serpiente
empalagarse
mudo
rumiar
amgdala
bonito
mucho
rebelde
nervio
adelgazar
algodon
azul
jadear
zampoa
oriente
martes
reprender
mandar
enviar
traer
cangrejo
cucaracha
llevar
chicha
teatro
fabula
lagarto
poesia
poeta
ramera
admitir
agonizar
reir
aumentar
maloliente
mudarse
acarrear
posible
atenerse
obstinado
triunfo
competencia
poderoso
poder
zorro
telar
tejedor
abuelo
enemigo
guerra
combatir
difunto
embalsamar
calavera
dolor
tumba
cutis
carnicera
parentesco
procesin
cosechar
abril
borroso
fugitivo
huir
enjuagar
puente
atragantarse
atravesado
trancar
pie
canilla
astuto
pez 
pescadera
precio
valorado
valioso
hacha
cascabel
sonajera
agricultor
mezcla
mezclar
eso
risueo
burlarse
polluelo
hembra
extraviar
orilla
llovizna
marca
medir 
inclinado
inclinarse
murcilago
pulsera
verdad
aclarar
ciertamente
perfectamente
recto
verificar
frio
invierno
imposible
enfriar
odioso
odiado
odiar
cabello
melenudo
sentarse
enfermizo
momia
callo
remojar
cinturn
cola
arrojar
alacena
contradecir
puesto
poner
hijo
frijol
liviano.
aspero
roca
anochecer
escarcha
hielo
pico
picotear.
pero
viudo
esteril
rayo.
jueves
resplandeciente
afortunado
cosa
criada.
emperador
combustible
sol
domingo
reloj
occidente
oeste
rodear
tia
pichon.
nio
adoptar
acariciar
colador
par
vacilar
doblar
segundo.
indeciso
podrido
chismoso
ortiga
retirar
retirarse
iguana
lastima,
vamos
gloton
collar
desdentado
manantial
venir
elevar
cielo
medicina
curarse
medicamentos
curandero
curado
medico
millon
rivalidad
aprender
aquel
alli
suave
levantado
superior
crecer
picante
superficie
cosmeticos
pomada
untar
que
aji
cuanto
cantar
favor
ayudar
manosear
alcanzar
asi
todo
erguirse
culpa
inculpar
culpable
buho
manantial
oh
liberar
soltar
despedida.
enviado
enviar
disparar
despedir
sal
salero
sazonar
bombo.
tio
quedarse
empedrador
adoquinar
fuerza
fortalecer
fornido
esforzarse
autoridad
ley
quemar
patio
asado
asador
asar
campo
despejar
borde
tener
toldo
corredor
lejos
alejarse
forastero
cerrajero
casera
monstruo
sustentar
amigo 
vida. 
mango
ser
abrir
abierto
destapar
igualado
identico
luna
lunes
menguante
nimbo
ladear
ladera
tres
triplicar
tapa
destapar
diente
suegra
coca
fantasma
pepa
tacto
red
palpar
morado
hoy
sobrino
cuello
occidente
fragancia
condor
perfumar
mayor
magnifico
alegrar
araa
alegrarse
dichoso
contento
alegremente
costal
molido
moler
vez
devolver 
defender
reponerse
regresar
mover
moverse
atemorizar
mordisco
tartamudo
encajar
tartamudear
brecha
adoqun
vereda
lanzadera
desportillar
masticable
bocado
morder
cabalgar
operar
sucio
temblar
temor
miedoso
langosta
omoplato
eructo
vanagloria
altanero
acaro
nudo
anudar
armadillo
espina
espinoso
cosquilla
frotarse
lija
lijar
enrollar
cerdo
ensuciarse
ensuciar
cortar
intruso
curiosear
gusano
juntos
mitad
reflexionar
examinar
esmalte
pintor
pintar
corroer
pena
amor
compasivo
silbar
oscuro
oscurecer
oscurecerse
deshojar
arder
rajado
rajar
hechicero
embrujar
embrujado
desollar
incipiente
pesar
mentirse
salir
apenarse
suspirar
compadecer
triste
pena
melancolico
triunfo
probar
sombra
sombrilla
sombrear
nublarse
total
flamear
peso
sangre
flojo
saliva
babear
carpintero
desbastar
bronce
ciudad
ciudadano
lamer
brillar
vestir
ulcera
caido
tocar
borrasca
inundar
axila
daga
mentira
encubrir
mentiroso
escaldar
engrasar
salida.
salirse
pues
ola
borracho
borrachera
viejo
vejez
envejecer
bisabuelo
entiesarse
puado
ratero
madre
maternidad
no
nada
nadie
desconocido
tampoco
amedrentar
espantapjaros
temible
asustarse
asustadizo
susto
olla
estupido
deuda
fiarse
prestado
acreedor
fiar
encerrar
porra
pelear
golpeado
urticaria
golondrina
abatir
pueblo
innumerable
gigante
lavarse
lavar
sedimentado
cualquiera.
rio
pastorear
alimentar
comida
comer
asquear
asqueroso
asco
lana
lanudo
salado
gato
extenderse
edad
veneno
achachi
achachila
achaki
achhuy
achhiy
achuy
ajina
ajllay
ajsu
ajta
ajwa
aka
akanawasi
akatanqa
akawara
akay
allin
allinchay
allinyachiy
allinyay
alqu
alquchakuy
alquchay
amaay
amaru
amichikuy
amu
amullay
amuxlli
ananay
ancha
ankalli
anku
ankuyay
anpi
anqa
ansaqiy
antara
anti
antichaw
anyay
apachimuy
apachiy
apamuy
apanqura
apatara
apay
aqha
aranwa
aranyay
ararankha
arawi
arawix
aricha
ariniy
arqhuy
asiy
askhayachiy
asna
astakuy
astay
atikux
atikuy
atipakuj
atipana
atipanaku
atipax
atiy
atuq
awana
awax
awki
awqa
awqanakuy
awqay
ayachukusqa
ayachukuchiy
ayauma
ayaw
ayawasi
aycha kara
aychawasi
ayllu
ayma
aymuray
aymuraykilla
ayphu
ayqix
ayqiy
aytiy
chaka
chakachikuy
chakasqa
chakay
chaki
chakisinqa
challi
challwa
challwaqhatu
chani
chaninchasqa
chaniyux
chanpi
chanrara
chaxcha
chaxraruna
chaxru
chaxruy
chay
chijchi
chijchipayay
chilwi
china
chinkachiy
chinkana
chinpara
chinpu
chinpuy
chinru
chinruy
chipana
chiqa
chiqachay
chiqallanmi
chiqamanta
chiqan
chiqanchay
chiri
chirimita
chirisunqu
chiriyay
chixnina
chixnisqa
chixnix
chuchu
chujcha
chujchasapa
chukuy
chullchu
chullpa
chullpi
chulluy
chunpi
chupa
chuqay
churana
churanakuy
churasqa
churay
churi
chuwi
chhalla
chhama
chhanka
chhapa
chhulla
chhullunka
chhuru
chhutuy
ichaqa
ijma
ikumi
illapa
illapachaw
illariy
illayux
ima
imilla
inka
inqhana
inti
intichaw
intiwatana
intiyaykuna
intiyaykuy
intuy
ipa
irpa
wanra
irqichakuy
irqichay
isanka
iskay
iskaychakuy
iskaychay
iskayiqi
iskaysunqu
ismusqa
isqallu
ithapallu
ithichiy
ithiriy
iwana
iyaw
jaku
jalki
jallmay
jallmu
jalsuri
jamuy
jananchay
janaxpacha
janpi
janpichikuy
janpina
janpiri
janpisqa
janpix
janu
japinakuy
japiqay
jaqay
jaqaypi
jasa
jatarisqa
jatun
jatunyay
jaw
jawa
jawikuna
jawina
jawiy
jay
jaya
maska
jaylliy
jayma
jaymay
jaywapayay
jayway
jina
jinantin
jinay
jucha
juchachay
juchayux
juku
juturi
juy
kacharipuy
kachariy
kacharpaya
kachasqa
kachay
kachaykuy
kachaypariy
kachi
kachichurana
kachinchay
kaja
kaka
kakuy
kallkix
kallkiy
kallpa
kallpachay
kallpasapa
kallpay
kamachix
kamachiy
kanay
kancha
kanka
kankana
kankay
kanpu
kanpuchay
kantu
kapuy
karpa
karpawasi
karu
karunchay
karuruna
kasachix
kasira
kawri
kawsachiy
masi
kawsay
kawu
kay
kichariy
kichasqa
kichay
kikinchasqa
kikinpacha
killa
killachaw
killachinkay
killaxjinpun
kinranchay
kinray
kinsa
kinsachay
kirpana
kirparay
kiru
kisma
kuka
kuku
luru
kullana
siru
kullay
kulli
kunan
kuncha
kunka
kunti
kuntu
kuntur
kuntuy
kurax
kusa
kusichiy
kusikusi
kusikuy
kusiruna
kusiy
kusiykusiylla
kutama
kutasqa
kutay
kuti
kutichipuy
kutikuy
kutiriy
kutiy
kuyuchiy
kuyuy
karkachiy
khachu
khaka
khakay
khakayay
khalla
khallki
khallkiy
khallma
khallpay
khamuna
khamuy
khaniy
khapaykuy
khariy
kharka
kharkatiy
kharkay
kharkiri
kharma
kharmin
khasay
khasikay
khaskikhaski
khiki
khipu
khipuy
khirkinchu
khiska
khiskakhiska
khiskilli
khitukuy
khituna
khituy
khiwiy
khuchi
khuchichakuy
khuchichay
khuchuy
khurku
khurkukuy
khuru
khuska
khuskan
khuskikuy
khuskiy
khusku
khuskux
khuskuy
khutuy
khuya
khuyay
khuyayniyux
khuyuy
laqha
laqhayachiy
laqhayay
laqhiy
lawray
laxra
laxray
layqa
layqay
layqaysqa
liqhiy
liwi
liaphiy
liasachay
liullakuy
llakikuy
llakipakuy
llakipayay
llakisqa
llakiy
llakiysapa
llalli
llamiy
llanthu
llanthucha
llanthuy
llanthuyay
tukuy
llaphapay
llasa
llawar
llawqha
llawsa
llawsay
llaxllax
llaxllay
llaxsa
llaxta
llaxtamasi
llaxway
llijuy
llikuy
llilli
lluchhu
llujchhikuy
llujllapara
llujllay
wayllaku
lluki
llulla
llullapay
llullasunqu
lluphiy
jawiy
lluxsina
lluxsipuy
ma
machapu
machasqa
machay
machu
machukay
machuyay
machuyaya
makhurkay
maki
makiyux
mama
mamakay
mana
manakax
manapipis
manarixsisqa
manatax
manchachi
manchachina
manchana
manchariy
manchaskiri
manchay
manka
mankata
manu
manukamuy
manusqa
manux
manuy
maphachay
maqana
maqanakuy
maqasqa
mara
marax
maray
marka
maychhika
maychhikan
mayllakuy
mayllay
mayqa
mayqin
mayu
michiy
mikhuchiy
mikhuna
mikhuy
millachikuy
millana
millay
millma
millmasapa
millu
michi
mismiy
mita
miyu
miyuy

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

Edited 6 Years Ago by Robert1995: wrong text

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?

Edited 6 Years Ago by mattjbond: n/a

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.

Edited 6 Years Ago by mattjbond: n/a

This article has been dead for over six months. Start a new discussion instead.