Need help counting brackets in my code

i need a program to read its own code and count the amount of open brackets inside it, and tell me the amount at the end. in class, i learned a code to make the program look inside itself, and sat "I found it!" whenever it saw one, so then i tried to make an int and increment it once foe each time it saw a bracket, but when i run the debugger, i just get a debug error, saying something about run- time check failure #3 - T. Here's my code so far, i don't really know why it isn't working.

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

#include "stdafx.h"
#include <iostream>//first thing to do
#include <fstream>//for reading the in and out
//true false to see if something is a {, }, or not?
#include <string>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
	string strFileName;
	string strLine;
	int v;
	strFileName = "FULL PATH FOR THE PROGRAM";//putting the path into the string so we can open it, cant open something if theres nothing inside.
	
	ifstream in(strFileName);//input stream called in, which is a stream of bits from strFileName file.
	//now we need to look for brackets
	while (!in.eof()){
		getline(in, strLine);
		cout << strLine << "\n";
		for (int i = 0; i < strLine.length(); i++)	{
			if (strLine[i] == '{') v++;
			cout << v;
		}


	in.close();
	return 0;
}






----------------------------------------------
UPDATE:
I found i was outputting v too early, i moved it down to after i closed the file, and now its telling me 14, which i believe is too many... any ideas on why it would give me a number thats too high?
Last edited on
Line 14: v is an uninitialized variable. Hint: it contains garbage.

Line 23: When you increment v, what do you think the value of v is the first time through the while loop?

Line 19: Using eof() here is incorrect. eof is set only AFTER attempting a read. Therefore if the getline at line 20 fails (sets eof), you make an extra pass through the loop with whatever happens to be in strLine from the last iteration. Lines 19-20 should be:
 
  while (getline(in, strLine))

v++ What value of v was before that? What it should be if this line is never executed? Show part of your code which ensures that.
Topic archived. No new replies allowed.