Difficulty loading a dictionary in to a char array

Hello, I'm struggling with classes and functions and am trying to load a dictionary in to a char array called Engelv. Here is a sample of the dictionary(as is in the txt file spaces including).

league laar
leagues laare
twelve ratse
one er
two atta
three nelde
four kanta
five lempe

My aim with the following code was to load the English words in to Engelv[1].... and to load its Elvish opposites in to Engelv[2]....

I'm also trying to print out the dictionary to very that it works but i'm getting nothing but junk values.



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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <iostream>
#include <fstream>
#include "Translator.h"

using namespace std; 

int main ()
{
	Translator translator("englishtoelvish.txt");
	translator.printEngelv();
	system("pause");
}


#include <iostream> 
#include <string> 
using namespace std; 
class Translator 
{
public:
	Translator(string dictName): Dictionary(dictName){ };
	char dictionary(string Dictionary);
	void printEngelv();
private:
	string Dictionary;
	char engElv[2][805][50];
};


#include <iostream>
#include <string>
#include <fstream> 
#include "Translator.h"

using namespace std; 

void Translator :: printEngelv()
{
	int i = 0;
	int j = 0;
	cout << "Go printInt!!!"<<endl;
	for (i = 0; i <= 2; i++)
	{
		for (j = 0; j<=805; j++)
		{
			cout << engElv;
			cout <<i<<endl;
			cout <<"Printing j : "<<j<<endl;
		}
	}
}
	




char Translator::dictionary	(string Dictionary)
{
	char first[50];
	char second[50];
	bool wWord = 0;
	char gotLine[100];
	fstream Dict;
	Dict.open(Dictionary, ios::in);
														   //Opens and checks that file was succesfully opened
  if (Dict.fail())										   //Adds the dictionary words in to the arrays
    {
      cout << "Could not open dictionary file" << endl;
      return -1;
    }																			
																 
   while (!Dict.eof())
    {

		Dict.getline(gotLine, 100, '\n');
		for (int i = 0; i <=strlen(gotLine); i++)
		{
			if (gotLine[i]==' ')
			{
				wWord = 1;
			}
		
			continue;
		}

		if (wWord==0)
			{
				first[50]=gotLine[100];
				for (int j = 0; j <=805; j++)
				{
					first[50] = engElv[0][j][50];
					cout <<first;
				}
			}

			else
			{
			second[50]=gotLine[100];
				for (int k = 0; k<=805; k++)
				{
					second[50] = engElv[1][k][50];
					cout <<second;
				}	
			}
    }
}

  
I think the bug is here:
first[50]=gotLine[100]; in line 88
and here
first[50] = engElv[0][j][50]; in line 91
and here
second[50]=gotLine[100]; in line 98
and here
second[50] = engElv[1][k][50]; in line 101

You are accessing 51st and 101st elements of 50-element and 100-element arrays, which could be junk data. Also both loops from lines 89 and 99 repetedly write to the same element in arrays first and second (or specifically just outside those arrays).
Last edited on
Topic archived. No new replies allowed.