English 中文(简体)
C+++ 打印要屏幕的文本文件的单个字符
原标题:C++ Printing out individual characters of a text file to screen
  • 时间:2012-05-25 17:21:21
  •  标签:
  • c++
  • vector

我有一个叫做"迷宫.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++的新人

最佳回答

因为这看起来像一个学习项目(家庭工作、自学、什么的),

我认为这句话(特别是评论)表明了问题所在:

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

您没有“ recursing” anything, 您却忽略了巴沙尔法的结果 。 您只能确定下一个字符是按字母顺序排列的, 然后没有使用它做任何事情, 而只有一个字符 。

问题回答

暂无回答




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签