Reading an input file into an array

Hey,

I feel like something weird is happening. Basically I have a file that looks like this;

2
1 2
2 1

And what I want to do is read the second and third lines into arrays;

int n;
inputfile >> n;
int * tarray;
int * parray;
tarray = new int [n];
parray = new int [n]; // Store other numbers in arrays
for(int i=0; i < n; i++)
{
inputfile >> parray[n];
cout << parray[n] << endl;
}
for(int i=0; i < n; i++)
{
inputfile >> tarray[n];
cout << tarray[n] << endl;
}

This seems to work fine, but then when I read out an array value (parray[0], say) after this, I get nonsense values. No idea why.
Last edited on
You are putting the values into parray[n] and tarray[n], not parray[i] and tarray[i].

Given that parray[2] will be beyond the bounds of the array, it could output anything (but it won't be what you want).
Oh my god. That is so stupid. Thanks!
Topic archived. No new replies allowed.