如果要检查这 2 个文件是否存在语法错误,VS c++ 代码的输入文件需要在哪里?

Where do input files for a VS c++ code need to be if these 2 files are to be checked for syntax errors?

提问人:Tim Read 提问时间:12/8/2022 更新时间:12/8/2022 访问量:42

问:

#include <iostream>
#include <fstream>
#include<stack>
#include<string>


using namespace std;
int main()
{
    stack<char> s;/*created a stack object with char data type*/

    string file_name;
    cout << "Enter the file name: ";
    cin >> file_name;

    ifstream testFile;
    testFile.open(file_name, ios::in);
    char ch;
    while (testFile.peek()!=EOF) 
    {
        testFile >> ch;
        if (ch == '[' || ch == '(') 
        {
            s.push(ch);
        }
        if (ch == ']')
        {
            if (s.empty())
            {
                s.push(ch);
            }
            if (s.top() == '[')
            {
                s.pop();
            }
        }
        if (ch == ')')
        {
            if (s.empty())
            {
                s.push(ch);
            }
            if (s.top() == '(')
            {
                s.pop();
            }
        }

    }

    if (s.empty())
    {
        cout << "No Syntax Error Occured.";
    }
    else
    {
        cout << "Syntax Error Occured.";
    }

    return 0;
}

大家好。这是我在这里的第一篇文章,我需要调试语法检查程序的帮助。我确定堆栈部分是正确的。我认为错误是由于输出没有显示“发生语法错误”或“未发生语法错误”字样,我仍在学习如何调试和欣赏任何建议。当 covid 刚出现时,我就开始了 IT,由于远程学习,我无法掌握基础知识。就像调试一样。

我期望“发生语法错误”或“未发生语法错误”的输出,但它只是以代码 0 退出。

C++ 调试 语法 错误 visual-studio-2019

评论

0赞 Thomas Matthews 12/9/2022
我不明白。编译器会自动执行语法检查,而不管文件位于何处。我错过了什么吗?
0赞 Yujian Yao - MSFT 12/9/2022
我测试了您上传的代码,它输出“未发生语法错误”。你能告诉我你在运行这段代码时输入了什么吗?

答: 暂无答案