Removing comments and whitespace from LOC Counter

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
  #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;
}
Perhaps you should read up on the find method of std::string.

Hint: It does not return a bool.
Last edited on
Topic archived. No new replies allowed.