2

每次我运行此方法时,它都会返回我输出的错误消息。例如:用户输入:

display <table.txt> sortedby <ID>

这是我希望用户在调用显示函数时使用的正确语法。但是当用户类型使用正确的语法显示时,它会输出我指定的错误消息。

 Syntax error: display <intable> sortedby <col_name>

总的来说,在这种方法中,我希望表格以漂亮的格式显示。但它不会超过 if 语句。我想知道是否有一些我忽略的东西可能会返回我的 error_message。

 void display(Lexer lexer) {
  Table table; // create a table from the created table class
  vector<Token> tokvec = lexer.tokenize();



// expect [IDENT | STRING] sortedby IDENT
 if (tokvec.size() != 4 ||
    (tokvec[0].type != IDENT && tokvec[1].type != STRING) ||
    tokvec[2].value != "sortedby" || tokvec[3].type != IDENT){
    error_return("Syntax error: display <intable> sortedby <col_name>");
    return;
 }


    string fn = tokvec[1].value; // name of the file
    string col_name = tokvec[3].value;
    table.set_input(fn);
    table.scan_input();
    table.set_index(col_name);
    table.sort();
    table.display();

}

4

1 回答 1

1

我建议您打印出 if 语句中比较的每个值。其中一个是真的,而它应该是假的,所以你需要找出它是哪一个。完成此操作后,找出不应该出现的值,并将错误追溯到其来源。

像这样的东西:

cout << tokvec.size() <<  " doesn't equal " << 4 << " - " << tokvec.size() != 4 << endl;
cout << tokvec[0].type << " doesn't equal " << IDENT << " - " << tokvec[0].type != IDENT << endl;

等等。

这将打印出每个正在做出的陈述,然后是它是否正确。它应该清楚问题出在哪里。

于 2012-10-30T04:29:49.287 回答