I have a map that contains Spanish words as keys and the same word in English as the values for each key. I want to translate a string of words from Spanish to English. I'm assuming I'll need to parse the string to separate the words. I don't know how to search through the map keys and then to display the value.
map<string, string> trans;
tran["rearrancar"] = "reboot";
tran["pantalla"] = "screen";
tran["texto"] = "text";
tran["virus"] = "virus";
tran["tinta"] = "ink";
tran["mitad"] = "half";
tran["interno"] = "internal";
tran["memoria"] = "memory";
tran["papel"] = "paper";
tran["energia"] = "power";
tran["fallo"] = "bug";
tran["pelo"] = "hair";
tran["el"] = "the";
tran["dos"] = "two";
tran["todas"] = "all";
tran["en"] = "in";
tran["de"] = "of";
tran["los"] = "the";
tran["comprar"] = "buy";
tran["tarde"] = "afternoon";
tran["quieres"] = "want";
tran["muchachos"] = "boys";
tran["tienen"] = "have";
tran["ordenador"] = "computer";
tran["con"] = "with";
tran["antes"] = "before";
tran["vacio"] = "empty";
tran["tu"] = "you";
tran["hambre"] = "hunger";
tran["contaminado"] = "corrupt";
tran["a"] = "to";
tran["una"] = "a";
tran["la"] = "the";
tran["cafe"] = "brown";
tran["su"] = "your";
tran["es"] = "is";
tran["quiero"] = "want";
tran["vamos"] = "go";
tran["mi"] = "my";
tran["barco"] = "ship";
tran["nosotros"] = "we";
tran["casa"] = "house";
tran["yo"] = "I";
tran["borrar"] = "delete";
tran["necesita"] = "necessary";
tran["despues"] = "after";
string paragraph ("yo quiero una ordenador virus
todas de los muchachos tienen interno memoria
mi pelo es cafe
tu quieres tinta con su papel
rearrancar el ordenador a vacio el pantalla");
Would it be better to store each word into an array of strings?
Edit: I can now search the map for the word to translate but it crashes after the 4th translated word. I'm sure it has something to do with the parameters in my for loop but I don't know what to put in it.
stringstream ss(paragraph);
string word = "";
for (int i = 0; i < paragraph.length(); i++) {
getline(ss, word, ' ');
cout << tran.find(word)->second << " ";
}
Paragraph is the string containing the paragraph to be translated. Tran is the name of my map containing the Spanish keys and English values.