1

したがって、次のような「maze.txt」というテキストファイルがあります。

###############
Sacbifnsdfweovw
###############

上から左下の文字が「S」かどうかを確認してから、その右下の文字が英字かどうかを確認したいと思います。次に、前の文字に続く文字がアルファベット文字であるかどうかを確認し続けます。これらの文字は私のベクトル「パス」に保存されます。最後に、これまでに使用した各アルファベット文字を画面に印刷して表示します。しかし、何らかの理由で、上記の文字のチャンクの場合、画面には「S」と「a」のみが印刷され、その他は印刷されません。どうしたの?これが私のコードです:

int main()
{
    ifstream file("maze.txt");
    if (file) {
        vector<char> vec(istreambuf_iterator<char>(file), (istreambuf_iterator<char>())); // Imports characters from file
        vector<char> path;                      // Declares path as the vector storing the characters from the file
        int x = 17;                             // Declaring x as 17 so I can use it with recursion below
        char entrance = vec.at(16);             // 'S', the entrance to the maze
        char firstsquare = vec.at(x);           // For the first walkable square next to the entrance

        // Check that the entrance really has 'S'
        if (entrance == 'S')                    
        { 
            path.push_back(entrance);           // Store 'S' into vector 'path'
        }

        // Check if the first path is an alphabetical character and thus walkable
        if (isalpha(firstsquare))               
        {
            path.push_back(firstsquare);        // Store that character into vector 'path'
            isalpha(vec.at(x++));               // Recurse 'isalpha' to the next adajcent square of the maze;
        }                                       // checking next character

        cout << "Path is: ";                    // Printing to screen the first part of our statement

        // This loop to print to the screen all the contents of the vector 'path'.
        for(vector<char>::const_iterator i = path.begin(); i != path.end(); ++i)  // 
        {
        std::cout << *i << ' ';
        }

        cout << endl;
        system ("pause");                       // Keeps the black box that pops up, open, so we can see results.
        return 0;
        }
}

前もって感謝します!ちなみに私はC++を初めて使用します。

4

1 に答える 1

3

これは学習プロジェクト (宿題、自己学習など) のように見えるので、ヒントだけを示します。

この行(特にコメント)が問題を示していると思います:

isalpha(vec.at(x++)); // Recurse 'isalpha' to the next adajcent square of the maze;

何も「再帰」しておらず、isalpha の結果を無視しています。次の文字がアルファベットかどうかを判断するだけで、それに対して何もせず、その1文字だけです。

于 2012-05-25T17:34:50.603 に答える