C++20 VS2022 program to count lines please

I need help in writing C++20 program using VS2022 please.

Program has to read any cpp/cc or hpp/h source file as input and
count blank lines, comment lines and count actual source code lines
and create corresponding output source file without blank lines and without comments.

This C++20 program has to use
exception handling to catch errors
fstream, iostream, filesystem etc include files etc.

If there is already an existing such a C++20 program, may I have its URL link please?

Then I will be adding some more features.

I have searched on google and did not find such c++20 program.

Thanks and best regards,


Last edited on
What is being defined as a comment line?

// comments the rest of the line following but can have valid source preceding.

/*....*/ comments a block of code between the /* and */ delimiters. This block can be part of a line or 1 or more lines.

So to comment comment lines, what is being meant needs to be known. Is this just lines starting with // or...
Also line continuation characters need to be processed.

Fortunately there are no trigraphs in c++20.
seems like a good place to use regex...
Last edited on
Just taking a comment line as beginning with //, ignoring line continuation (treat them as separate lines) and ignoring /*..*/, then as a starter for 10:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <string>
#include <iostream>
#include <cctype>

int main() {
	unsigned blank {}, comment {}, code {};

	for (std::string line; std::getline(std::cin, line); ) {
		if (const auto ret { line.find_first_not_of(" \t") }; ret == std::string::npos)
			++blank;
		else
			if (line.size() >= 2 && ret < line.size() - 2 && line[ret] == '/' && line[ret + 1] == '/')
				++comment;
			else {
				std::cout << line << '\n';
				++code;
			}
	}

	std::cerr << blank << " blank lines\n";
	std::cerr << comment << " comment lines\n";
	std::cerr << code << " code lines\n";
}


Usage:

c:\MyProgs>test65 < example.cpp > example.out
7 blank lines
1 comment lines
20 code lines


example.cpp

#include <iostream>
#include <fstream>
#include <string>
#include <iostream>
using namespace std;

const string fnam = "test12.cpp";

//This is a test comment line
           
int main()
{
string s;
ifstream myfile(fnam.c_str());

        if (!myfile.is_open()) {
                cout << "Could not open file\n";
                return 1;
        }

int count = 0;

        while (getline(myfile, s))
                count += (s.length() && !(s[0] == '/' || s[0] == ' ' || s[0] == '#'));

        cout << "This is your LOC: " << count << endl;
        return 0;
}


example.out

#include <iostream>
#include <fstream>
#include <string>
#include <iostream>
using namespace std;
const string fnam = "test12.cpp";
int main()
{
string s;
ifstream myfile(fnam.c_str());
        if (!myfile.is_open()) {
                cout << "Could not open file\n";
                return 1;
        }
int count = 0;
        while (getline(myfile, s))
                count += (s.length() && !(s[0] == '/' || s[0] == ' ' || s[0] == '#'));
        cout << "This is your LOC: " << count << endl;
        return 0;
}

Last edited on
Hello seeplus,
Thanks a bunch for program code.
This code is a great start for me to add more features to it.

Again, many thanks for your excellent help.


Hello mbozzi and jonnin,
Thanks so much for your excellent coding inputs.

Best regards,
Don't forget to test with inputs like
1
2
cout << "This is a C comment /* comment */" << endl;
cout << "This is a C++ comment // comment" << endl;
Topic archived. No new replies allowed.