usingnamespace std;
class loc
{
public:
int count();
//int counter(string fn);
int methodcount(string fn);
//int object(string fn);
private:
string line;
int objectLOC;
int method;
};
int loc::count()
{
string filename;
cout << "Enter the file you want to count lines of code for(include extension): ";
cin >> filename;
//counter(filename);
methodcount(filename);
//object(filename);
}
int loc::methodcount(string fn)
{
ifstream file;
string line;
int methodcount = 0;
while(getline(file, line))
{
if(line.find("::"))
{
methodcount++;
}
}
cout << methodcount << " number of methods"<<endl;
return methodcount;
}
/*
int loc::object(string fn)
{
}
*/
int main()
{
loc loc;
int val= loc.count();
return 0;
}
It compiles and runs but return a method value of 0. Why isn't it outputting anything? If I remove the while loop and just leave the if it returns a method value of 1.
This part should be left to the user of your program no? If the user supplied a file that contains no method, then your program is correct. But if the user supplies a file that contains no methods and your program is returning 1, then there is something you have not accounted for in the program.
Also note that string.find does not return zero to indicate false. It returns string::npos to indicate not found
Actually returning something from your "count()" member function would be a good place to start. Your main function is calling "count()" which in turn calls "methodcout()" but "count()" does not return anything.
Nope, code in C++ will only do what you tell it to. Unless you tell the function specifically to return something, it isn't going to. Computers are great at following directions, but they are stupid too.