dynamic array has junk digits attached to beginning of each element
I have been experimenting with dynamic arrays, but have run into a problem with the output. Whenever I read in the numbers from a file, they are correct (output them to the screen to check), but once the movieID gets stored in the array the number "8224" gets appended to the beginning of the number. The output to a small subsection looks like this:
822430 3
8224157 3
8224173 4
8224175 5
82248 5
822428 4
822430 5
822483 5
82241144 4
82241202 5
82241428 4
82241518 4
Code:
struct userStruct
{
unsigned int length;
unsigned long userID;
unsigned int* movieID;
unsigned short* rating;
};
unsigned long nVal = 480000; //I had to do this for the program to execute
userStruct* user;
user = new userStruct[nVal];
unsigned int tempLength;
string line, l1, l2;
unsigned long tem;
unsigned int tem1;
unsigned short tem2;
unsigned long counter = -1;
while (!inFile.eof())
{
getline(inFile, line);
if (line.find(':') != string::npos)
{
counter++;
getline(inLen, line);
tem = atoi(line.c_str());
user[counter].movieID = new unsigned int[tem];
user[counter].rating = new unsigned short[tem];
user[counter].length = 0;
}
else
{
l1 = line.substr(0, line.find(' '));
l2 = line.substr(line.find(' ')+1, line.length()-line.find(' ')-1);
tem1 = atoi(l1.c_str());
tem2 = atoi(l2.c_str());
tempLength = user[counter].length;
user[counter].movieID[tempLength] = tem1;
cout<<endl<<"** "<<tem1;
user[counter].rating[tempLength] = tem2;
cout<<' '<<tem2;
user[counter].length++;
}
}
for( int i = 0; i<3; i++)
{
cout<<endl;
for (int j = 0; j< 4; j++)
cout<<' '<<user[i].movieID[j]<<' '<<user[i].rating[j]<<endl;
}