I'm working with this peace of that count the logical lines in a program, omitting comments and black lines. The counting line is working, but I don't know how to omit the comment lines, I try
if (line == "//")
{
comment++;
}
but it only checks for lines that start with "//"
and if there's text next to that it doesn't count that as a comment line :/
At the end when I know the total lines and total comment lines, I will subtract totalLines-commentLines
to know the real program number lines.
Can someone help me with this?
Thank you
#include <iostream>
#include <fstream>
#include <istream>
using namespace std;
int main()
{
int numlines = 0;
int comment = 0;
string line;
ifstream myfile("wr.cpp");
while (myfile.good())
{
getline(myfile, line);
if (line == "//")
{
comment++;
}
if (line == "/**/")
{
comment++;
}
++numlines;
}
cout << "Number of lines: " << numlines << endl;
cout << "Number of comment lines: " << comment << endl;
return 0;
}