Out of scope and can't fix

When I compile my code it keeps giving me an error I can't seem to fix.

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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#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;
}


I get this error:

test.cpp: In member function ‘int loc::counter(std::string)’:
test.cpp:51: error: ‘filename’ was not declared in this scope

You have are using an undeclared variable.
file.open(filename.c_str());

Maybe you meant fn instead of filename
Well that certainly fixed it haha thanks giblit.
Actually how does fn work? Where did I declare this?

I thought file name was declared by
string filename;
Its the parameter you are passing.

int loc::counter(string fn)
The only place I can see that you've declared a variable called filename is inside loc::count(). Within the scope of loc::counter(), you haven't declared anything with that name.
Last edited on
Topic archived. No new replies allowed.