0

I created an english-spanish translator and need to make it translate spanish to english from an input file. I used vectors to read the english translations from the file i have which is called dictionary. So an example of the dictionary file would be black, negro blue, azul green, verde

and so on.

heres a link to the dictionary file I have set up

http://pastebin.com/iuF5EfTZ

I prompt the user to input the word and it will translate. I figured out how to use the english-spanish vector first. I tried to switch around my for loop thinking it would work but I realize my vector for loop doesn't read right to left. So how do you make a vector read right to left from an input file? I implemented a boolean so when the word or translation has been found it will cout it, but since it doesn't read right to left it wont cout. Need help thanks.

To view my full program to test it heres a link:

http://pastebin.com/KHhiwLJs

below is the code block for the vector that needs to be read right to left /////////////////////

void spanish()
{
ifstream fin;
vector <string> eng;
vector <string> span;
string words,temp;
bool FOUND = 0;




fin.open("dictionary.txt");
if (fin.fail())
{
    cout << "Failed";
    exit(1);
}


 do{

    getline(fin, words);
    eng.push_back(words);
    getline(fin, words, ',');
    span.push_back(words);

 }while (!fin.eof());

    cout << " Spanish-English Now type in a Word you want to translate\n";
    cin >> words;

    for (int i=0; i < words.length(); i++)
        words[i]= toupper(words[i]);

    cout << "you entered " << words << endl;


    for (unsigned int i=0; i<span.size(); i++)
    {
        if (span[i]==words)
        {
            cout << "Spanish translation is \n" << eng[i] << endl;
            FOUND = 1;
        }
        if (FOUND)
            break;
    }

/////////////////////////////////////////// THIS VECTOR NEEDS TO READ RIGHT TO LEFT!!!

 fin.close();


}
  • 3
    Would a reverse iterator work for you? http://www.cplusplus.com/reference/vector/vector/rbegin/ – druckermanly Dec 14 '14 at 09:36
  • Please explain what you mean exactly by reading right to left. – n. m. could be an AI Dec 14 '14 at 09:40
  • i tried the reverse iterator I dont know how to implement it correctly, and everything in code is read left to right in the compiler. So when I input a file it is reading the file I have left to right. In my inputfile its english to spanish. I need to have Spanish to english so the file that is read to be english to spanish needs to be read spanish to english. – Ryan Weaver Dec 14 '14 at 09:42
  • 1
    It seems like you should use a `std::map`, not two `std::vector`s, to implement a translation dictionary. – Barmar Dec 14 '14 at 09:42
  • @barmar thats way out of my knowledge, I am a beginner at c++ my class ends next week for it and this is the last homework due. and if I am able to have the program translate from one file i get extra credit – Ryan Weaver Dec 14 '14 at 09:44
  • It's still unclear what the problem is. The file has two words on each line, the first is English, the second is Spanish. You put the English word in `eng`, you put the Spanish word in `span`. To do a translation, you search for the word in whichever vector is the source language, and print the corresponding word in the other vector. There's no left and right when doing this, there's just two vectors. – Barmar Dec 14 '14 at 09:46
  • The problem seems to be that you're using `getline` for each word. `getline` reads a whole line, not just one word. You need to read the line, then split it at the comma, and put the first word into `eng`, the second word into `span`. – Barmar Dec 14 '14 at 09:48
  • @barmar If there is no left or right, right to left, then how come in my Spanish code block the Boolean isn't true and doesn't translate spanish to english? Take a look at my full code and try the spanish to english translation. – Ryan Weaver Dec 14 '14 at 09:52
  • @barmar I do have a comma splitting it lol its not translating spanish to english but it will translate english to spanish man. test it out http://pastebin.com/KHhiwLJs – Ryan Weaver Dec 14 '14 at 09:53

1 Answers1

1

You're using the comma delimiter in the wrong call to getline. The comma is after the English word, not the Spanish word. So it should be:

getline(fin, words, ',');
eng.push_back(words);
getline(fin, words);
span.push_back(words);

Also, there's a space between the comma and the Spanish word, you need to remove that. See this question for ways to remove whitespace surrounding a string:

What's the best way to trim std::string?

Community
  • 1
  • 1
Barmar
  • 741,623
  • 53
  • 500
  • 612