-1

我正在编写一个 C++ 程序来显示文本文件中引号或单行或多行注释中的任何文本。问题是我不能让它正常运行。这是我的程序:

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

int main () {
    char ch, name[20];
    int count=0;
    fstream fin;
    cin>>name;
    fin.open(file_name, ios::in);
    while(!fin.eof()) {
        fin.get(ch);
        if(ch=='/') {
            fin.get(ch);
            if(ch=='*') { 
                a:
                fin.get(ch);
                while(ch!='*') {
                    cout<<ch;
                    fin.get(ch);
                }
                fin.get(ch);
                if(ch!='/') goto a;                                 
            }
            if(ch=='/') {
                fin.get(ch);
                while(ch!='\n') {
                    cout<<ch;
                    fin.get(ch);              
                }
            }
        }
    }

    getch(); 
    return 0;
}

编辑:它陷入无限循环。我知道如何。问题仍然存在。我正在使用 Dev C++ 编辑器。

4

1 回答 1

0

无限循环需要循环。您的代码中有两个循环。第一个while(!fin.eof()) {不会导致无限循环,因为文件最终会被耗尽。

所以我们仍然使用内部循环和 goto 语句。我认为逻辑中有一个错误 - 如果星号后没有斜线,则循环将永远不会结束。

于 2012-04-20T12:40:12.280 回答