I have an assignment for my Software Engineering class and I've almost got it working except for a stupid flaw in my code that I can't find.
I'm supposed to design and implement a program that counts the logical lines of any file passed into it omitting comments and blank lines. My program works except that it counts 2 more lines than it should. If someone could help debug my program, I'd greatly appreciate it.
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <stdio.h>
usingnamespace 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;
}