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

Recommended Answers

All 5 Replies

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?......

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

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?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.