Pregunta

I'm trying to create a program which can automatically solve word search puzzles.

I'm having some trouble storing the inputted word search puzzle. The example word search text file I'm supplied with contains letters separated by two spaces. There is a blank line between each populated line.

This is the code that I have right now. It seems to only store and output the left most column of the puzzle. What am I doing incorrectly? Is there a cleaner way of doing this?

vector< vector <string> > puzzle;
ifstream myPuzzle ("puzzle.txt");
if (myPuzzle.is_open())
{
    while (myPuzzle)
    {
        string temp;
        if (!getline(myPuzzle, temp))
            break;

        istringstream ss(temp);
        vector <string> record;

        while (ss)
        {
            string temp2;
            if (!getline(ss, temp2, ' '))
                break;
            record.push_back(temp2);
        }

        puzzle.push_back(record);
    }
}
else
    cout << "Failed to open puzzle file!" << endl;

cout << "Puzzle succesfully inputted" << endl;

for (vector< vector<string> >::size_type i = 0; i < puzzle.size(); ++i)
{
    for (vector<string>::size_type it = 0; it < puzzle[i].size(); i++)
        cout << puzzle[i][it];
    cout << endl;
}

Example matrix of characters (puzzle.txt):

U  T  Y  Y  A  L  P  S  G  I  B  S  K  I  P  Q  H  S  K  T

A  T  L  T  I  R  U  N  E  Z  I  M  O  D  N  A  R  S  F  P

D  J  I  P  R  H  H  V  E  U  E  W  U  K  F  D  L  T  A  V

R  T  N  Q  N  O  E  Y  U  O  L  H  D  S  P  Q  C  A  F  H

A  A  E  U  O  O  S  A  J  F  B  O  I  Z  S  C  C  C  V  L

T  T  A  I  D  P  I  B  P  L  B  L  C  E  T  H  P  K  T  I

I  I  R  C  U  P  Q  S  A  W  U  B  T  Y  N  P  E  R  T  Y

C  S  T  K  U  M  S  C  R  O  B  I  I  K  N  I  L  L  H  R

D  E  N  B  N  A  K  I  R  U  O  N  O  H  L  I  Q  Y  L  A

F  L  A  M  I  Z  M  M  E  S  C  S  N  S  B  B  H  O  H  N

U  E  T  W  O  F  O  H  D  E  M  E  A  G  E  T  E  S  X  I

R  C  S  X  N  I  V  T  A  I  E  R  R  Z  Y  G  K  V  M  B

C  T  N  F  I  V  C  I  G  O  R  T  Y  X  E  H  X  G  W  D

D  I  O  T  W  D  Y  R  O  I  G  I  Z  L  V  L  P  V  Q  N

J  O  C  S  E  W  L  A  U  R  E  O  U  Q  W  O  S  A  T  I

I  N  O  I  U  M  F  G  B  D  L  N  C  C  Y  O  N  M  R  F

L  K  P  L  E  D  U  O  I  A  M  O  R  T  I  Z  E  J  B  G

M  S  Y  Y  U  R  V  L  Q  D  F  P  T  E  E  R  T  K  C  W
¿Fue útil?

Solución

You use i++ in both for loops. The second one should use it++.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top