123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
#include <iostream> #include <string> #include <fstream> #include <cstdlib> #include <cstring> #include <vector> /*Testing Block Comment*/ using namespace std; class loc { public: int count(); int counter(string fn); //int methodcount(string fn); //int object(string fn); private: string clas; //int objectcount; //int method; }; int loc::count() { string filename; cout << "Enter the file you want to count lines of code for(include extension): "; cin >> filename; clas.clear(); counter(filename); //methodcount(filename); //object(filename); } int loc::counter(string fn) { ifstream file; string line; file.open(filename.c_str()); int positioncount = 0; int remove = 0; bool bc = false; // bc = block comment bool C = false; //c = class while(getline(file, line)) { if(line.length()==0) { remove++; } else if(line.find("//") != std::string::npos && line.find("\"//\"") == std::string::npos) { remove++; } else if(line.find("/*") != std::string::npos && line.find("\"/*\"") == std::string::npos) { remove++; bc = true; } else if(line.find("*/") != std::string::npos && line.find("\"*/\"") == std::string::npos) { remove++; bc = false; } else if(line.find("{") !=std::string::npos && line.find("\"{\"") == std::string::npos) { remove++; } else if(line.find("}") !=std::string::npos && line.find("\"}\"") == std::string::npos) { remove++; } else if(bc) { remove++; } else positioncount++; } cout << "There are " << positioncount << " lines of code." << endl; return positioncount; } int main() { loc locObject; int val= locObject.count(); return 0; }
file.open(filename.c_str());
fn
filename
string filename;
int loc::counter(string fn)