Program not doing cout

So my program compiles and runs correctly but it doesn't do what it's supposed to at the end. It is supposed to display the number of methods in my own code but nothing shows up when ran.

What am I missing here?


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
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <cstring>
#include <vector>

using namespace std;

class loc
{
	public:
	int count();
	//int counter(string fn);
	int methodcount(string fn);
	//int object(string fn);


	private:
	
	vector<string>LOC;
	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::methodcount(string fn)
{
	if(clas.empty())
	{
		return 0;
	}
	int methodcount = 0;
	string methodfinder = clas + "::";
	
	for(unsigned int i=0; i<LOC.size(); i++)
	{
		int pos = LOC[i].find(methodfinder);
		if(pos != string::npos)
		{
			methodcount++;
		}
	}
		cout << methodcount << " number of methods"<<endl;
		return methodcount;
}

int main()
{
	loc locObject;
	int val= locObject.count();
	return 0;
}


Last edited on
The problem is very easy to spot when you walk though it step by step. If you have difficulty doing this "by eye", I recommend learning to use a debugger and step over each line of code so you can see how it's executing.


That said... take a look at what is happening:

1) main does nothing other than creating the locObject object... then calling count().

2) count gets a filename from the user

3) count then clears your 'clas' string, so it is now empty

4) count calls methodcount()

5) methodcount() checks your 'clas' string to see if it is empty. It is (because you just cleared it --- see step 3).. so methodcount immediately exits.

6) Once methodcount exits... count exits... then main and your program exits.



So basically the problem is that methodcount is expecting your 'clas' string to not be empty... but it will always be empty because you never put any data in it.
Try going through it step by step:
1) You create a 'loc' (LOC is the empty vector, clas is the empty string, method is undefined)
2) You ask the user to enter a filename
3) You clear clas (effectively does nothing, since it is already empty)
4) You call methodcount(filename)
5) In methodcount(), you check if clas is empty (it is), and so you return 0
6) Back in count(), you don't return anything (though you have declared it to return an int! This is bad!)
7) Back in main(), you place this anti-returned-value of nothing into val (luckily you don't try to use it)
8) You program exits with return code 0

Edit: ninja'd
Last edited on
Ok so I've commented out clearing my clas.
And I've also commented out the check. Those were left there for the other counter that I didn't include in this code to make it shorter.

So now it does cout the number of methods but it returns a value of 0. Why does this happen?
You're looking for method names inside your LOC object.

But your LOC object is also empty because you never put anything in it. You're you're looking for method names in something that doesn't have any data.
Topic archived. No new replies allowed.