2

For my CS class, we've been given an assignment to create an english to spanish translator using the text file from here: http://www.ilovelanguages.com/IDP/files/Spanish.txt

So far, I've got the the insertion down, but when I run the program - words get mingled up and I'm betting that the fact the txt file has multiple words for each line is what's ruining it

#include<iostream>
#include<string>
#include<fstream>
using namespace std;

class transl
      { private:
            class word
                  { public:
                        string eng_word,
                               spa_word;
                        word * next,
                             * prev;    

                        word(string engl_word, string span_word, word * next1 = NULL, word * prev1 = NULL)
                        {   eng_word = engl_word;
                            spa_word = span_word;
                            next = next1;
                            prev = prev1;   }
                  };

            word * head,
                 * tail;

        public:
            transl::transl()
            {   head = NULL;
                tail = NULL;    }

            transl::~transl()
            { word * wordPtr, * nextWord; 
                     wordPtr = head;

                     if(head == NULL)
                        return;

                     while(wordPtr != NULL)
                          { nextWord = wordPtr->next;
                            delete wordPtr;
                            wordPtr = nextWord; }
            }

            void insert(string engl_word, string span_word)
            {   if(head == NULL)
                   head = new word(engl_word, span_word);

                    else
                    {   word * wordPtr;
                        wordPtr = head;

                        while(wordPtr->next != NULL)
                        {     wordPtr->next->prev = wordPtr;
                              wordPtr = wordPtr->next;  }

                        wordPtr->next = new word(engl_word, span_word);
                        cout << wordPtr->next->eng_word << "    " << wordPtr->next->spa_word << endl; } 
            }

            string search(string engl_word)
            {   return "Hello"; }

            };

int main()
    {   ifstream inFile;    inFile.open("dictionary.txt");
        string engl_word, span_word;
        transl test;

            if (!inFile)
            {   cout << "ERROR! CANNOT LOCATE DICTIONARY!" << endl;
                system("pause");
                return 1;   }

            cout << "Loading Dictionary:  ";

                while(inFile >> engl_word >> span_word)
                {   test.insert(engl_word, span_word);  }

            cout << "Done!" << endl;
            system("pause");
            system("CLS");

            cout << "Translate: ";
             cin >> engl_word;

            cout << "The translation of " << engl_word << " is " << test.search(engl_word) << '.' << endl;

            system("pause");
        return 0;   }

an example of what it looks like is this:

peor    wound
la    herida
wounded    herida
Done!
Press any key to continue . . .

Would anyone know the procedures to insert the values properly

Nightfirecat
  • 11,432
  • 6
  • 35
  • 51
Janian
  • 21
  • 1

1 Answers1

2

wordPtr->next->prev = wordPtr; is superfluous and confusing. You are searching the end of the list, but modifying it in the process.

You are forgetting to write the prev pointer of the new word in insert.

However, the real problem is in reading the dictionary. When you perform inFile >> engl_word >> span_word, two whitespace-limited lists are read in. However, the spanish entries are not whitespace-limited, but take up the rest of the line. While you can read the english word with >>, you'll need something like getline( cin, span_word ) to parse the spanish expression.

thiton
  • 35,651
  • 4
  • 70
  • 100