I'm trying to strip the code of all comments. However, as you can see, it doesn't work right. Anyone know the problem?
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
string str = "", main = "";
ifstream in("file.txt");
int pos;
while(!in.eof()) {
in.getline((char *) str.c_str(), 255);
pos = str.find("//");
if(pos != string.npos) {
main += str.erase(pos, str.length());
} else {
main += str;
}
main += '\n';
}
cout << main << endl;
in.close();
cin.get();
return 0;
}
I'm running it with this as file.txt:
Code:
one; // comment 1
two;
r;
four;