I have written this code to remove the comments from an input file and then put it into an output file. The program compiles and runs but it removes the whole line of code when it encounters a /* */ type comment. I believe the problem is in the while loop but I just don't know where. I am really new to programing and any help would be greatly appreciated.
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
int main(void)
{
string infile, outfile;
ifstream source;
ofstream target;
cout<<"Please enter the file name you would like the comments removed from: "; /* Infile to have comments removed */
cin>>infile;
cout<<"Please enter the file name where you would like the file without comments to go: "; /* Outfile with all the comments removed */
cin>>outfile;
source.open ( infile.c_str() ); /* Returns a constant char array containing the characters stored in infile, stopped by a null character. */
target.open ( outfile.c_str() ); /* Returns a constant char array containing the characters stored in outfile, stopped by a null character. */
string position;
bool notice = false;
while ( ! source.eof() ) /* While loop to make sure the whole input file is read */
{
getline(source, position); /* To read line by line. */
if (position.find("/*") < position.size() ) /* searching for /* to remove it and everything it contains */
notice=true;
if (!notice)
{
for (int i=0;i<position.size();i++)
{
if(i<position.size()) /*sees if i is less than the length of position */
if ((position.at(i) == '/') && (position.at(i+1) == '/')) /* searching for // and then erase the comments after. */
break;
else
target << position[i]; /* puts the character into the i position */
}
target<<endl;
}
if (notice)
{
if ( position.find("*/") < position.size() )
notice = false;
}
}
cout<<"The output file now has no comments in it while your input file is unchanged.\n";
source.close(); /* Close the files I opened */
target.close();
return 0;
}