Comment Stripper - Logic Errors

Hello all, this is my first post here. I have been enrolled in a C++ class for about 3 weeks, and found this site after my first class and have been reading the references and source code very frequently. We received our first assignment which was to write a comment stripper in Visual Studio 2008.

The guidelines of the project were to remove // comments, /*...*/ comments, and ignore // or /*...*/ if inside of a string.

As of right now, my code is removing comments from inside a string. I'm just inputting the code of program itself, and writing out to a text file. What is outputting is...


1
2
string example;
example = "text 



When what I need it to output is

1
2
string example;
example = "text//text";



For the other two conditions, I used .peek and .get. So, summed up, if input.get == '/' then, depending on what the next character was, either a / or a *, it would do its thing.

The issue I can't seem to comprehend is getting the program to 'see' a set of quotation marks, and output the line until it hits the closing quotation marks, ignoring everything in between.

I looked at functions here and what I've discovered is string::find, and string::find_last_of. But the quotation marks will always be at the end of the line, ignoring carriage return.

I guess what I'm asking is if someone can point my thinking to something relevant. What I have so far(and please don't laugh, I'm sensitive;) ) is for checking the quotation marks, however it is just deleting everything after the // in a string as per above example.

1
2
3
4
5
6
7
while (charCurrent == '"'){
   charNext = input.peek();
   getline(input, str);
	if(charNext != '"'){
	getline(input, str);
	}
}



Any guidance that can be given would be greatly appreciated, and I apologize in advance for writing the above novel.


Thanks in advance:)



Last edited on
Well, I would check for the '"' first, then if it is, iterate through the rest of the string (or lines if needed) until you find another '"'. If you don't, you could return an error or something. Also, you might also want to make sure the next " isn't a \", which is not the end of a string.
Thanks for the fast reply!!

The way I'm doing it now is (paraphrased) is it loops while the input isn't at the end of file. First it checks for quotes, and keeps whatever is in the quotes, then checks for /, and depending on what is after that, the program either skips the line or skips the set of lines.

I think the //comment removal and the quotation marks are "fighting" each other and the // is winning. I know there is a major flaw, I just can't seem to pick it out.

What does \" do, let you carry a string to the next line? I'm still a noob :)
Topic archived. No new replies allowed.