Hi guys,
I have been having trouble initialising a 2D vector.
I'm trying to fill the vector with a text.txt file which has the following format.
12543
14235
37855
12344
etc.
I need to create a 2D vector array form the file and print it to cout<<
I have the following code written and it compiles okay.
but when I go to print the array and check its size ie theARray.size().
after the function it says 0.
can someone please have a quick look at the code and possibly see why it wont create an array?
I can't see anything obviously wrong (but I may be missing the same thing as you:-)
What I would do is add some cout statements inside the function so I could see better what is happening.
EG
in >> aValue;
if (in.eof()) return;
cout << aValue << endl;
aRow.push_back(aValue);
}
in.get(separator);
cout << "Push Back Row" << endl;
locArray.push_back(aRow);
}
aArray = locArray;
in.close();
cout<<"\n"<<"this is the aArray size "<<aArray.size();
}
This the output generated by the function cout<< statements
I beleive Grey Wolf is right that the fuction doesn't make it to line 27 but I'm not sure why. As you can see it never gets to display the aArray size to cout<<
once again thanks for your help I'll keep tyring
Brendan
1 12543
Push Back Row
1 14235
Push Back Row
3 37855
Push Back Row
1 12344
Push Back Row
ÿ do you know what this character is ??
Hi guys,
Almost there.
I changed the return statement to a break and added another break and eof() command.
The problem left is that it only works when the text.txt characters are separated ie 5 6 7 8 9 not 56789.
Any ideas would be appreciated
regards
Brendan
The problem is that this line in >> aValue;
is telling the program to read a double value from standard input.
If you want to pull in only one character at a time, you need to send it to a char variable instead. You could do this for example:
1 2
in >> separator;
aValue = separator - '0';
The second line converts an ASCII number to an actual numerical value. Keep in mind however that if the user enters something that isn't a number, it'll choke. You may want to check that separator >= '0' && separator <= '9'
Never trust the user not to do something stupid.
Hope this helps.
P.S.: Doing it this way makes your previous in.peek() statement a little redundant. It would be more efficient to reduce lines 15-21 as follows: