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!