I've got an assignment for my Software Engineering class that's driving me bananas. I've been asked to design a line counter that counts only the logical lines of code for any given file. It must omit blank lines and comments.
I've got the code pretty much working except for that it over counts the line numbers by 2 lines no matter what file I pass into it. I can't for the life of me see where my problem is and was wondering if anyone could help me out.
Here's my code:
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <stdio.h>
using namespace std;
int main () {
// Initialize variables
ifstream infile;
string filename;
int line = 0;
// Get file input
cout << "Enter the filename" << endl;
cin >> filename;
// open the file
infile.open(filename.c_str());
// read the lines and skip blank lines and comments
while(getline(infile, filename)) {
if(filename.empty() || filename.find("//") == true) {
continue;
}
// increment the line number
++line;
}
// close the file
infile.close();
// display results
cout << "There are " << line << " lines of code in this file." << endl;
}
The counter reads as follows in the terminal: "There are 24 lines of code in this file."
According to my calculations there should only be 22 lines of logical code. Help would be appreciated.