Problem with a program.

This code is supposed to remove the comments from the input file and then put it into an output file. It does everything correctly except removing the /* */ type comments. When it gets to those it just removes the whole line and that is not correct. Could someone please give me a hand with this? I will be grateful for any help.

_____________________________________________________________
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
57
#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.length() ) /* 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 s */

     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.";
source.close(); /* Close the files I opened */
target.close();
return 0;
}
could you include a copy of the input file please
The input file can be any c++ file. A program like the one right here. I've tried it with multiple files and it does the same thing to all of them. This is a file though that i used with it. The program i states in the original post would erase the whole line with a comment on it that was /* */ like that.

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/* Program to ask user to fill 2 matrices and then
   add the matrices together and display the results.
   Written by Patty
   February 25, 2013
   Language: c++
   Target g++
   Written in Dognet
*/

#include <iostream>
#include <iomanip>
using namespace std;

int main(void)
{
int n, x;

cout<< "Enter the number of rows you would like: \n";
     cin>>n;
cout<< "Enter the number of columns you would like: \n";
     cin>>x;

int matrix1[n][x],
    matrix2[n][x],
    matrix3[n][x];


cout<< "Enter the integers to fill matrix number 1. Press enter after each integer. \n";  /* This takes the values for matrix 1*/
   for (int i=0;i<n;i++)                            /* i is row and j is column*/
   {    for (int j=0;j<x;j++)
             cin>>matrix1[i][j];
   }

cout<< "This is matrix number 1. \n"; /* This prints out the values of matrix 1*/
for (int i=0;i<n;i++)
   {   for (int j=0;j<x;j++)
       {
          cout<<matrix1[i][j]<<" ";
       }
     cout<<endl;
   }

cout<< "Enter the integers to fill matrix number 2. Press enter after each integer. \n"; /* This takes the values for matrix 2*/
  for (int i=0;i<n;i++)
   {   for (int j=0;j<x;j++)
            cin>>matrix2[i][j];
   }
cout<< "This is matrix number 2. \n";  /* This prints the values for matrix 2*/
for (int i=0;i<n;i++)
   {   for (int j=0;j<x;j++)
       {
          cout<<matrix2[i][j]<<" ";
       }
     cout<<endl;
   }
   
for(int i=0;i<n;i++) /* This adds matrix1 with matrix2 and puts the values in matrix3*/
{
    for(int j=0;j<x;j++)
   {
         matrix3[i][j]=matrix1[i][j]+matrix2[i][j];
         cout<< endl;
    }
          
}
cout<< "This is the two matrices added together. \n"; /* This prints the matrix3*/
for (int i=0;i<n;i++)
   {   for (int j=0;j<x;j++)
       {
          cout<<matrix3[i][j]<<" ";
       }
     cout<<endl;
   }

return 0;
}
For something of this nature, you want to process the file character-by-character, not line-by-line.

How would you handle:

1
2
3
4
/* This is
   a multi-line
   comment
*/
im honestly not sure. i understand how i would want to read in each character though. is there going to be a lot of changes to the code i have now or is it something simple? I'm sorry i'm really new to all this.
I believe this will do the trick

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
using namespace std;

void openFile(ifstream& in, ofstream& out);       //opens an input and output file
void removeComments(ifstream& in, ofstream& out); //removes comments from a file

int main(void)
{
ifstream source;
ofstream target;

openFile(source, target);
removeComments(source, target);

cout<<"The output file now has no comments in it while your input file is unchanged.";
source.close(); /* Close the files I opened */
target.close();

cin.ignore(1000, '\n'); //keeps the window open
return 0;
}/*endl of main*/

/******************************************************
* opepns an input and output file
*
*******************************************************/
void openFile(ifstream& in, ofstream& out)
{/*beginning of openFile */

	char myAnser = 'N';
	char anser;
	string infile, outfile;

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;

in.open ( infile.c_str() ); /* Returns a constant char array containing the characters stored in infile, stopped by a null character. */
out.open ( outfile.c_str() ); /* Returns a constant char array containing the characters stored in outfile, stopped by a null character. */
	
//checks for valid input
while(in.fail())
{/*beginning of while loop*/

	cout << "The file you specified failed to open" << endl;
	cout << "Do you wan't to continue [y/n]" << endl;
	cin >> anser;
	anser = toupper(anser);
	if (anser == 'N')
		exit(0);
	else if (anser == 'Y')
	{
		in.close();
		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;

		in.open ( infile.c_str() ); /* Returns a constant char array containing the characters stored in infile, stopped by a null character. */
		out.open ( outfile.c_str() ); /* Returns a constant char array containing the characters stored in outfile, stopped by a null character. */
	}
	else
		cout << "Invalid input" << endl;

}/*end of while loop*/
}/*end of function openFile*/

/*******************************************************
* removes comments from a file
*
********************************************************/
void removeComments(ifstream& inFile, ofstream& outFile)
{

char firstChar, secondChar;
string discard;
int index;
while ( ! inFile.eof() ) /* While loop to make sure the whole input file is read */
{
	//finds the first occurance of / character
	firstChar = inFile.get();
	if (firstChar == '\n')
		outFile << firstChar;
	else if (firstChar == '/')
	{
		//determins if this position is the beginning of a comment
		secondChar = inFile.get();
		if ((firstChar == '/') && (secondChar == '/'))
		{
			getline(inFile, discard);
		}
		else if ((firstChar == '/') && (secondChar == '*'))
		{
			while (firstChar != '*' && secondChar != '/')
			{
			firstChar = inFile.get();
				if (firstChar == '*')
				{
					secondChar = inFile.get();
				}
				else if (secondChar == '/')
				{
						break;
				}
			}/*end of inner while loop*/
		} 
	}
	else 
		outFile << firstChar;

}/*end of outer while loop*/
		
}/*end of function removeComments*/
Topic archived. No new replies allowed.