Problem with filling 2D Vector Array

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?

kindest regards
Brendan


Heres the code



typedef vector<double> Colour2DArrayRow;
typedef vector<Colour2DArrayRow> Colour2DArray;


void load(const string& fileName, Colour2DArray& aArray)
{
ifstream in(fileName.data()); //inputs file text.txt
assert(in.is_open());

Colour2DArray locArray; //lcoal array to hold values
double aValue;
char separator;

for(;;)
{


Colour2DArrayRow aRow;
for(;;)
{


separator = in.peek();
if (separator == '\n') break;
in >> aValue;
if (in.eof()) return;
aRow.push_back(aValue);
}
in.get(separator);
locArray.push_back(aRow);
}
aArray = locArray;
in.close();
}





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
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
void load(const string& fileName, Colour2DArray& aArray)
{
    ifstream in(fileName.data()); //inputs file text.txt
    assert(in.is_open());

    Colour2DArray locArray; //lcoal array to hold values
    double aValue;
    char separator;

    for(;;)
    {
        Colour2DArrayRow aRow;
        for(;;)
        {
            separator = in.peek();
            if (separator == '\n') break;
            cout << separator << "\t";
            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();
}


NB: Use the # button to format code as above:-)
Last edited on
closed account (z05DSL3A)
From the looks of it, you return from the midle of the function and never get to line 27, so all the work is lost.
Last edited on
I think I can see your problem. Take a closer look at line 3:

ifstream in(fileName.data()); //inputs file text.txt

string::data() returns a (const char *) pointer to the data, but it is not NUL terminated. Try string::c_str() instead:

ifstream in(fileName.c_str()); //inputs file text.txt
Thanks for your resonses guys,
I've tried what you've said by chage the code to this


//This is the test.txt file

34567
12345
24356
12345
43567


//function to load vector with data

void load(const string& fileName, Colour2DArray& aArray)
{
ifstream in(fileName.c_str()); //inputs file text.txt
assert(in.is_open());
Colour2DArray locArray;
double aValue;
char separator;

for(;;)
{
Colour2DArrayRow aRow;
for(;;)
{
separator = in.peek();
if (separator == '\n') break;
cout << separator << "\t";

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



for(;;)
{
Colour2DArrayRow aRow;
for(;;)
{
separator = in.peek();
if (separator == '\n') break;
cout << separator << "\t";

in >> aValue;
if (in.eof()) break;
cout << aValue << endl;

aRow.push_back(aValue);
}
if (in.eof()) break;

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();

}




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:
1
2
3
4
5
6
7
8
in >> separator;
if(separator == '\n') break;
cout << separator << '\t';
if(in.eof()) break;
/* check for error here */
aValue = separator - '0';
cout << aValue << endl;
aRow.push_back(aValue);
Last edited on
ÿ is possibly an end of file marker. seeing as it appears at the end of your list and isnt associated with the data.
Topic archived. No new replies allowed.