Reading Textfile into 2D array wordsearch

I'm having trouble reading my textfile into a 9x9 wordsearch grid. So far it just reads the textfile as 1 line.

dictionary.txt:
COMPILE
COMPUTER
DEBUGGING
HELLO
LANGUAGE
GRAPHICS
LOOP
PRINT
PROGRAMME
WORLD

wordsearch_grid.txt:
9
E M M A R G O R P
C L U A U N L L D
O T A O F I L O I
M E U N J G E O K
P W H K G G H P Q
I C O M P U T E R
L L V R Z B A O X
E H O M L E Q G U
T N I R P D C O E


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
109
110
111
#include "WordSearch.h"
#include "fstream"
#include "iostream"
#include "string"
#include "vector"
#include "array"


using namespace std;

vector<string> list;
vector<string> grid;
string line;
string n;

WordSearch::WordSearch(const char * const filename) 
{
	
	
}

WordSearch::~WordSearch() 
{
	

}

void WordSearch::ReadSimplePuzzle() {
	
	ifstream inFile;
	const int row = 81;
	string rowGrid[row];

	const int coloumn = 9;
	string coloumnGrid[coloumn];

	string dummyFile;

	inFile.open("wordsearch_grid.txt");
	inFile.ignore(1000, '\n');

	if (inFile.fail())
	{
		cerr << "Error Wordsearch Grid File" << endl;
		exit(1);
	}
	else
	{
		for (int i = 0; i < row; i++)
		{
			inFile >> rowGrid[i];

			cout << " " << rowGrid[i];

		}

		for (int j = 0; j < coloumn; j++)
		{
			inFile >> dummyFile;
			inFile >> coloumnGrid[j];
			cout << " " << coloumnGrid[j];
		}
		
		cout << endl;
		//while (getline (inFile, n))
		//{
		//cout << n << endl;			
		//}
		////grid[4][3];
		inFile.close();
		////cout << grid << endl;
		cout << "\n" << endl;
	}
	
}

void WordSearch::ReadSimpleDictionary() 
{
	ifstream inFile;
	inFile.open("dictionary.txt");
	

	if (inFile.fail())
	{
		cerr << "Error Dictionary File" << endl;
		exit(1);

	}
	else
	{
		int count = 0;
		while (getline(inFile, line))
		{
			list.push_back(line);
			cout << line << endl;
		}
		inFile.close();
	}

	
}


void WordSearch::ReadAdvancedDictionary() {
	
}

void WordSearch::SolvePuzzleSimple() {
	

}

Last edited on
What exactly does your input look like?
tpb my apologies, I've added the input file to the top of the post.
Having a separate row and column grid doesn't really make sense.
You can represent the 9x9 square of letters by a single vector of strings.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
    ifstream fgrid("wordsearch_grid.txt");
    fgrid.ignore(9999, '\n');

    vector<string> grid(row);

    for (int r = 0; r < row; r++) {
        grid[r].resize(column, '.');
        for (int c = 0; c < column; c++)
            fgrid >> grid[r][c];
    }

    // To print
    for (int r = 0; r < row; r++)
        cout << grid[r] << '\n';


Those variables at the top of your listing above are suspicious. They probably shouldn't be there. Use local varibles and member variables. And grid should probably be a member variable of the WordSearch class.
Last edited on
Topic archived. No new replies allowed.