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.
#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>
usingnamespace 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?
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: