Program problem.

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <fstream>
#include <string>

using namespace 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;
}
Last edited on
position.find("/*") indeed finds the position where "/*" is located. So you should use the result.

This is supposed to remove /**/ comments
1
2
3
4
5
6
7
8
9
10
11
12
13
14
string::size_type begin_comment = position.find("/*");
if(notice || (begin_comment != string::npos)) // Note that npos determines not found
{
  notice=true;
  if(begin_comment == string::npos)
    begin_comment = 0;
  string::size_type end_comment = position.find("*/");
  if(end_comment != string::npos)
  {
    end_comment -= begin_comment;
    notice=false;
  }
  position.erase(begin_comment, end_comment);
}
Not tested!
could you possibly show me how to put that code into the code i already have? i'm not sure how to do it and still have the existing code work.
It's supposed to be rather easy:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
while (getline(source, position)) // This makes sure that the loop ends when an error occurs (eof or any other)
 {
string::size_type begin_comment = string::npos;
if(!notice) // <------------
{
begin_comment = position.find("/*");
if(begin_comment != string::npos)
{
  notice=true;
     target << position.substr(0, begin_comment);
}
else
     target << position <<endl;
}
if(notice)
{
  if(begin_comment == string::npos)
    begin_comment = 0;
  string::size_type end_comment = position.find("*/");
  if(end_comment != string::npos)
  {
     target << position.substr(end_comment) <<endl; // <------------
    notice=false;
  }
}

 }

Still not tested!
Topic archived. No new replies allowed.