I'm reading in a .txt file, the first couple of lines are:
Burglar Earthquake Alarm John Mary
The first line is the number of variables which are listed in the second line.
When reading in the second line i am using stringstream to break the line up into individual variables and store them. my code is:
int main(int argc,char* argv)
varData varArr[numVars]; //create array to hold varData structs
for (i=0;i<numVars;i++) //set data to zero
//read in variable names
if(String.length()>1) //skip over empth lines
cout<<"String is "<<String<<endl;
iss << String;
cout<<"Token is "<<token<<endl;
cout<<"Token is "<<token<<" and varsRead is "<<varsRead<<endl;
the output this is generating is:
1 2 3 4 5 6 7 8 9 10 11
String is Burglar Earthquake Alarm John Mary
Token is Burglar
Token is Burglar and varsRead is 0
Token is Earthquake
Token is Earthquake and varsRead is 1
Token is Alarm
Token is Alarm and varsRead is 2
Token is John
Token is John and varsRead is 3
Token is Mary
and varsRead is 4
I cant explain the last 2 lines of output, I've got the token 'Mary' as its printed out in the second last line, then in my code i assign it to the struct variable then try to cout it again and its gone? its not even outputting the first half of the cout? how can that be?
The token 'Mary' is not being assigned to the struct either.
I'm guessing it has something to do with 'Mary' being the last token in the string because when i run the same thing on a file with a different number of variables, it always seems to lose the last one.
numVars is found after reading in the first line from the file, after this it will remain unchanged.
The file format is known, so i can be sure that it will exit the while loop when varsRead=numVars.
The behavior seems like there's a wild pointer somewhere and memory corruption, but I don't see anything in the code you provided that would cause this. Have you tried doing a clean followed by a build to make sure nothing is getting gunked up in the toolchain somewhere?
The following issues aren't likely to be causing your problem, but they deserve a quick comment or two
are not legal C++. The size of an array must be a compile time constant in C++. GCC has a compiler extension enabled by default that allows it -- you may want to disable it.
Why do you need a numParents member in varData? Can't you just check the size of parents (parents.size()) if you need that info?
Why are you using stringstream here? Your file is completely space delimited which is what the extraction operator was made for. Using stringstream makes things a bit more complicated than they need to be: