调试我的程序

Debugging my program

本文关键字:程序 我的 调试      更新时间:2023-10-16

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

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();

}

我建议您在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;

等。

这将打印出所作的每个语句,然后打印出是否为真。它应该清楚问题在哪里。