程序正在忽略输入 我试图用C++编写一个简单的解释器。到目前为止,它工作得很好,但它忽略了字符输入命令(',')

程序正在忽略输入 我试图用C++编写一个简单的解释器。到目前为止,它工作得很好,但它忽略了字符输入命令(','),c++,input,interpreter,brainfuck,C++,Input,Interpreter,Brainfuck,口译员: #include <iostream> #include <fstream> #include <windows.h> using namespace std; #define SIZE 30000 void parse(const char* code); int main(int argc, char* argv[]) { ifstream file; string line; string buffer;

口译员:

#include <iostream>
#include <fstream>
#include <windows.h>
using namespace std;

#define SIZE 30000

void parse(const char* code);

int main(int argc, char* argv[])
{
    ifstream file;
    string line;
    string buffer;
    string filename;

    cout << "Simple BrainFuck interpreter" << '\n';
    cout << "Enter the name of the file to open: ";
    cin >> filename;
    cin.ignore();

    file.open(filename.c_str());
    if(!file.is_open())
    {
        cout << "ERROR opening file " << filename << '\n';
        system("pause");
        return -1;
    }
    while (getline(file, line)) buffer += line;

    parse(buffer.c_str());

    system("pause");
    return 0;
}
void parse(const char* code)
{
    char array[SIZE];
    char* ptr = array;

    char c; 
    int loop = 0;
    unsigned int i = 0;
    while(i++ < strlen(code))
    {
        switch(code[i])
        {
            case '>':       ++ptr;  break;
            case '<':       --ptr;  break;
            case '+':       ++*ptr; break;
            case '-':       --*ptr; break;
            case '.':
                cout << *ptr;
                break;
            case ',':
                cin >> *ptr;
                break;
            case '[':
                if (*ptr == 0)
                {
                    loop = 1;
                    while (loop > 0)
                    {
                        c = code[++i];
                        if (c == '[') loop ++;
                        else if (c == ']') loop --;
                    }
                }
                break;
            case ']':
                loop = 1;
                while (loop > 0)
                {
                    c = code[--i];
                    if (c == '[') loop --;
                    else if (c == ']') loop ++;
                }
                i --;
                break;
        }
    }
    cout << '\n';
}

有人知道是什么原因导致它跳过输入字符吗?

我先看看这个:

unsigned int i = 0;
while(i++ < strlen(code))  // increments i NOW !
{
    switch(code[i])        // uses the incremented i.
你会看到:

DEBUG [1:46:.]
DEBUG [2:0: ]

你需要推迟递增
i
,直到你完成它。

它在哪一行中断?它不会中断太多,只是跳过了输入,对不起,任何歧义。如果你的解释器避免在其名称中使用亵渎的话,我们会有所帮助:(我从标题中删除了这句话,因为它与你的实际问题无关。谢谢!这比我想象的要明显得多:IBUG总是比你想象的更明显,一旦你看到它们。
unsigned int i = 0;
while(i++ < strlen(code))
{
    cout << "DEBUG [" << i << ":" << (int)code[i] << ":" << code[i] << "]\n";
    switch(code[i])
DEBUG [1:46:.]
DEBUG [2:0: ]