我有一个叫做"迷宫.txt"的文本文件 看起来是这样的:
###############
Sacbifnsdfweovw
###############
I want to check whether the letter on the left one down from the top is S , and then to check whether the one to the right of that is an alphabetical character. Then, I ll keep checking whether the characters that follow the previous letter are alphabetical characters. These letters are to be stored in my vector path . Finally, I ll print to screen each of the alphabetical letters we ve been through. But for some reason, for that chunk of letters above, the screen only prints out S and a , and none of the others. What s wrong? Here is my code:
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++的新人