Thread: Dictionary Translation Problems

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    1

    Dictionary Translation Problems

    Hey guys, I am working on a translation/transliteration program, that reads an english story, and translates it to elvish, using an english/elvish dictionary. After the code shown below, i explain the error that i receive.


    I have alot of code, I am not sure if i should post it all, but I will post what I think should be sufficient. I only just started to learn C++ 2 months ago, and have about 2 months experience at C, yet we have to do this difficult problem.


    There is a main file, a header file with two Classes: Translator and Dictionary, and a cpp file to implement the class functions.


    I have a constructor that reads in the dictionary file to dictFileName and copys the english words into englishWord, and the elvish words into elvishWord

    Firstly, headers included:


    Code:
    #include <iostream> 
    #include <fstream>
    #include <sstream>
    #include <string>
    #include <cstring>
    #include <string.h>
    #include <stdio.h>
    #include<ctype.h>
    #include <algorithm>
    #include <iterator>
    #include <vector>
    using namespace std;
    #include "Translator.h"
    The translator function:


    Code:
    Translator::Translator(const char dictFileName[]) : dict(dictFileName)
    {  
        char englishWord[2000][50]; 
        char temp_eng_word[50];
        char temp_elv_word[50];
        char elvishWord[2000][50];
        int num_entries;
    
    
        fstream str;
        
        str.open(dictFileName, ios::in);
        int i;
    
    
        while (!str.fail())
          {
           for (i=0; i< 2000; i++)
              {
              str>> temp_eng_word;
              str>> temp_elv_word;
              strcpy(englishWord[i],temp_eng_word);
              strcpy(elvishWord[i],temp_elv_word);
               }
    
    
            num_entries = i;
            
          }
                         
          str.close();
          
    }
    In the main file, the english lines are read into the toElvish function, and split into an array of words, temp_eng_words.


    Within this toElvish function, I am calling another function; translate, which reads in temp_eng_words and is supposed to return the elvish words:


    Code:
    char Translator::toElvish(char elvish_line[],const char english_line[]) 
    
    
    {
        int j=0;
                  
        char temp_eng_words[2000][50];
        //char temp_elv_words[2000][50]; NOT SURE IF I NEED THIS
        
        std::string str    = english_line;
        std::istringstream stm(str);
        string word;
        while( stm >> word) // read white-space delimited tokens one by one 
        {
           int k=0;
           strcpy (temp_eng_words[k],word.c_str());
                  
           k++;
           
        }      
        
        for (int i=0; i<2000;i++)
        {
        Dictionary::translate (out_s,temp_eng_words[i]);
        }
        
    }
    This is the translate function:


    Code:
    char Dictionary::translate (char out_s[], const char s[])
    {
    
    
     int i;
         
         for (i=0;i < numEntries; i++)
         {
            if (strcmp(englishWord[i], s)==0)
            break;
          }
        
            if (i<numEntries)
            strcpy(out_s,elvishWord[i]);
    }
    My problem is that when I run the program, I get the error
    Code:
    out_s was not declared in this scope'
    I have tried to declare char out_s in this scope, but this throws up a lot more errors.


    If you have read all of this, thanks; any suggestions/clues would be much appreciated.


    If it helps, here is the header file:


    Code:
    #include <iostream> 
    #include <fstream>
    #include <string>
    #include <string.h>
    #include<stdio.h>
    using namespace std;
    
    
    const int MAX_NUM_WORDS = 2000;
    const int MAX_WORD_LEN=50;
    
    
    class Dictionary 
    {
    public:
        
        Dictionary(const char dictFileName[]);
        char translate(char out_s[], const char s[]); 
    
    
    
    
    private:
            
        char englishWord[MAX_NUM_WORDS][MAX_WORD_LEN]; 
        char elvishWord[MAX_NUM_WORDS][MAX_WORD_LEN];
        int numEntries; 
        
    };
    
    
    class Translator
    {
    public:
        
        Translator(const char s[]);
        char toElvish(char elvish_line[],const char english_line[]);
        char toEnglish(char english_line[],const char elvish_line[]); 
        
        private:
        Dictionary dict;
    };
    And i guess, since I have space, for ultimate clarity I will include the main file (which cannot be changed!)

    Main File:

    Code:
    #include <iostream>
    #include <fstream>
    #include "Translator.h"
    
    
    using namespace std;
    
    
    // maximum number of characters in a line of the text
    const int MAXLINE=1000;
    int main(int argc, char *argv[])
    {
      if (argc<2)
        {
          cout << "No story file specified." << endl;
          return -1;
        }
    
    
      fstream infile;
      infile.open(argv[1], ios::in);
    
    
      if (infile.fail())
        {
          cout << "Could not open the story file." << endl;
          return -1;
        }
    
    
      Translator translator("englishtoelvish.txt");
      fstream outfile;
      outfile.open("story_in_elvish.txt", ios::out);
      if (outfile.fail())
        {
          cout << "Could not open the output file." << endl;
          return -1;
        }
    
    
      char english_line[MAXLINE], elvish_line[MAXLINE];
    
    
      // Translate the story into Elvish
      while (!infile.fail())
        {
          infile.getline(english_line, MAXLINE, '\n');
          if (!infile.fail())
        {
          translator.toElvish(elvish_line, english_line);
          outfile << elvish_line << endl;
        }
        }
      outfile.close();
      infile.close();
    
    
      // Read the translated story and re-translate into English
      infile.open("story_in_elvish.txt", ios::in);
      outfile.open("story_backto_english.txt", ios::out);
      while (!infile.eof())
        {
          infile.getline(elvish_line, MAXLINE, '\n');
          if (!infile.fail())
        {
          translator.toEnglish(english_line, elvish_line);
          outfile << english_line << endl;
        }
        }
    
    
      infile.close();
      outfile.close();
    }
    Last edited by Need a Hand; 05-09-2013 at 02:08 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > And i guess, since I have space, for ultimate clarity I will include the main file (which cannot be changed!)
    There's nothing to stop you writing your own main for test purposes.
    Code:
    int main ( ) {
        Dictionary dict("englishtoelvish.txt");
        char result[100];
        dict.translate(result,"boot");
        cout << result << endl;
    }
    Translate depends on Dictionary, so it makes sense to make sure your foundations are working properly before trying to build on top of them.

    When you think you've finished, then you use the main() provided by your tutor.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Clearly, you are aware of std::string, so why are you using char arrays? Get rid of them. They are unsafe and an eyesore.
    If you need arrays, you should look into std::vector and std::array (C++11), as better alternatives instead of C arrays. Here is some more info on safe use of arrays: SourceForge.net: Safer arrays in Cpp - cpwiki
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. translation api
    By herWter in forum C Programming
    Replies: 4
    Last Post: 12-11-2008, 02:29 PM
  2. translation!!
    By impossible in forum C++ Programming
    Replies: 14
    Last Post: 05-31-2008, 05:17 PM
  3. C to C++ translation help?
    By bill.thompson65 in forum C++ Programming
    Replies: 5
    Last Post: 10-21-2007, 12:56 PM
  4. Dictionary into a 2d Char Array... Problems.
    By Muzzaro in forum C Programming
    Replies: 10
    Last Post: 12-02-2006, 12:34 PM
  5. Translation
    By GaPe in forum C Programming
    Replies: 10
    Last Post: 04-04-2002, 09:45 AM

Tags for this Thread