Program doesn't work correctly

So I made a program that I keep continually modifying but as of right now I am using it to count my methods.

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
using namespace 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.

How do I fix this?
cppnoob25 wrote:
How do I fix this?

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

http://www.cplusplus.com/reference/string/string/npos/
Last edited on
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.
Last edited on
Ok so for test purposes lets say I use this program on itself. Then it should return 3 methods as it stands.

How can I make it so that it enters the loop if it finds a double semicolon?
So that it increments the method count?
::


I thought count did return something since it calls methodcount which is supposed to return methods.
Last edited on
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.
Topic archived. No new replies allowed.