A txt file which has in each line a name of an athlete and his 10 best scores for this year. Every athlete's name is no more than 20 chars and the number of the lines is unknown but less than 100.
the exercise asks to: store the names and the scores in arrays. Each collumn in the array that holds the scores, represends each athlete (collumn 1 of array scores, represent athlete 1, collumn 2 represends athlete 2 and so on)
Im stuck on how i can calculate the lines of the text, so the arrays that im gonna fill with names and scores wont have "rubbish" after the lines of my text finish. for example if i only have 50 lines in my text, i cant declare arrays of 99 positions coz after line 50 my program will enter "rubbish" in my arrays.
I suppose you could check char by char for new line characters.
1 2 3 4 5 6 7 8
int lines = 1;
ifstream myFile("filename.txt");
myFile.seekg (0, ios::beg); // this might be unnecessary, I would assume it starts at the beginning
while(myFile.good())
if ((char)myFile.get() == '\n')
++lines;
// note: now failbit is set