我正在尝试做一个从先前编写的程序中删除注释的项目。从我的理论上讲,我认为它应该可以工作,但是由于某种原因,输出文件最后总是空的...请以任何方式提供帮助...(PS对不起,如果缩进草率,复制和粘贴,似乎从来没有对我很好

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

using namespace std;


void comment_destroyer(ifstream&,ofstream&);


int main (void) {


string filestart;
string fileend;
ifstream start;
ofstream end;


do {
    cout<<"Name of the file you want to remove comments from: ";
    cin >> filestart;
}
while ( start.fail() );
cout << "What is the name of the file you want to call the stripped code?: ";
cin >> fileend;

start.open ( filestart.c_str() );
end.open ( fileend.c_str() );

comment_destroyer (start, end);

start.close();
end.close();

return 0;
  }

//
//Start of functions
//

void comment_destroyer(ifstream& start, ofstream& end){
    string line;
    bool found = false;
    int i=0;

    while (! start.eof()){
        getline(start,line);

        if (line.find("/*")<line.length())
        found = true;
        if (!found){
            for (int i=0;i<line.length();i++)
                {
                    if(i<line.length())
                    if ((line.at(i)=='/') && (line.at(i+1)=='/'))
                    break;
                    else
                    end<<line[i];
                }

        end<<endl;

        }
    if (found)
        {
            if (line.find("*/")< line.length())
            found == false;
        }
    }
}

最佳答案

以下部分是错误的。而不是将false分配给found,而是使用相等运算符。

if (found)
    {
        if (line.find("*/")< line.length())
        found == false;
    }
}

==更改为=
if (found)
    {
        if (line.find("*/")< line.length())
        found = false;
    }
}

关于c++ - 从C++字符串中删除注释,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22449488/

10-12 22:28