Ok so im totally new to C++......we use Dev in our labs
I cnt seem to figure out how to store the column of a text file into an array. I understand how to input the file usinf ifstream.The columns have Lastname(initial), Id#, Age and Gpa.
i thought js doin

while (inFile>>ws && !infile.eof())
{
inFile>> Lastname[index];
cout << Lastname[index]<<endl;
index++;
}
would give me an array called Lastname. I am required to store each column into an array.
Can anyone help me please
----------------
Listening to: Collin Raye - If u get there b4 i do
via FoxyTunes

----------------
Listening to: Jack Johnson - Wrong turn
via FoxyTunes

----------------
Listening to: Jack Johnson - Wrong turn
via FoxyTunes

i would use the getline function and make the delimiter the space between each element then store them in separate arrays

What if i use the inFile>>
like this

do
{
    inFile >> LstNme>> Id>> Age>> Gpa ;
    for (int i=0;i<index;i++)
    {
        LstNme=LastName[i];
        cout << LastName[i];
    }
}
while(inFile>> ws &&!inFile.eof());

wats wrong with this?......

Edited 3 Years Ago by Reverend Jim: Fixed formatting

The bitwise operation

inFile >> LstNme>> Id>> Age>> Gpa ;

should work.

Wish I could say the same the rest of the loop.

LstNme=LastName[i];

If youre trying to save LstNme in the Lastname-array, these should be the other way around.

the for-loop doesnt really do anything, except save the same LstName variable index amount of times in to the array.

You should either make more arrays to save all the data ( name, id, GPA ) or make your own information structure, containing those datas, and creating an array of that type.

I ended up doing this

while(inFile>>ws && !inFile.eof())
    {
        inFile >> ch;
        inFile >> Id;
        inFile >> Age;
        inFile >> Gpa;
        LastName[index]= ch;
        //IdNum[index]=Id;
        /*StudentAge[index]= Age;
        StudentGpa[index]= Gpa; */
        index++;
    }

but i can only get the LastName array.... the other ones when taken out of comments give me gibberrish.......i will continue trying

Edited 3 Years Ago by Reverend Jim: Fixed formatting

What sort of gibberish? :)

Are you sure the arrays are defined and visible in your function? And are you sure you are trying the insert the right sort of data in to the arrays?

This article has been dead for over six months. Start a new discussion instead.