Search within lines/Count specific lines

An assignment I am currently doing for my class involves transcribing lines of a text file to another while numbering lines with executable code and not numbering lines that are blank or contain only a comment.

Everything seems to be going well, but there are two parts I cannot figure out.

First, I cannot figure out how to establish a line that is only a comment. My first instinct was to simply do this by selecting any line that begins with the double slash:
1
2
3
4
5
if(line.substr(0, 2) == "//")
{
	cout << "        " << line << endl;
	test2 << line << endl;
}


But then I remembered that our teacher prefers things to be tabbed in order, so this would (I assume) no longer apply when a line begins with two tabs. In order to fix this, I tried doing a search within each line:
1
2
3
4
5
if(binary_search(line+start, line+end, "//"))
{
	cout << "        " << line << endl;
	test2 << line << endl;
}

but this gives me an error saying that 'start' and 'end' were not declared in this scope. We haven't discussed binary searches in class and I only implemented this after finding it online, so I'm sure I'm making some simple mistake.

Which method would be better, and how can i fix it to work correctly?


Second, I am supposed to, at the end of the transcription, add a line with a count of all of the comment lines and another line counting all of the executable lines. I really have no clue how to do this... anyone have any suggestions?

Thanks!
I would just search for the occurrance of "//" in the string and not copy that line (ie, until the program encounters the new line char "\n"). By copying the entire string (ie, one line at a time into the buffer), you can search for the //, and copy everything before it, but stop copying for that line when it encounters it (that way, if you add the comments at the end of the line, it'll still get the code before it, but not afterwards).

As for the counting of the number of lines of actual code and the number of comment lines, you can put a separate counter variable to run for each line of actual code, and each line of comment, so you can display these at the end of your program.

~maingeek
I think might have poorly articulated what i'm looking for on the first part - I'm not looking to copy everything but the comments, i'm looking to number lines with code and copy the lines as well with comments, just without a numbering.

It should also be useful to point out that our teacher doesn't want the comments to be written on the same line as code - all comments get their own line.


I'm working on implementing your second suggestion and i think i have it ok - as soon as i figure out how to properly transcribe it the way it's supposed to.

Thanks!
Another thing i could do is maybe extend my first coding for the "//" search so that it ignores all tabs and spaces until the first character appears. How do i do this exactly?
Don't forget the case where comments span multiple line with /* */ as start and end. Not sure if line in between have an * or not. Example:
1
2
3
4
5
6
7
8
9
10
11
12
#include iostream.h
#include something.h
using namespace std;
int main ()
{
    code
    code
/* comment, comment 
and more comments.  not sure if this line needs to start with *, 
and finally the end */
    code
}
Topic archived. No new replies allowed.