Dictionary Translation problem

May 9, 2013 at 7:18am
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
#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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 '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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#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();
}


This is quite a bit of code, more than half of which was given to us to start with, I am a beginner so I am probably missing out on something small.

Any tips or suggestions would be greatly appreciated :), thanks!
Last edited on May 9, 2013 at 7:34am
Topic archived. No new replies allowed.