1

I am having a hard time with an assignment, I am given a .txt file with data in the following format:

Michael Schumacher
Allemagne; 250 91
1994 1995 2000 2001 2002 2003 2004
Benetton; Ferrari;
Fernando Alonso
Espagne; 132 21
etc...

I need to read this file and funnel the data in appropriate variables:

full name                                   ----> to string
country; numberOfRace numberOfVictories     ----> to string; to int, to int
all years separated by spaces               ----> to an array
Sponsor1; [Sponsor2(optional)];             ----> to an array
full name2.....

I have spent 3 days trying to assign the data properly with a mixture of getlines and file >> and can't manage to get the array right, here my most recent attempt:

for (int i = 0; i < NPILOTES; i++) {
        getline(fichier, liste.pilotes[i].nom);
        getline(fichier, liste.pilotes[i].pays, ';');

        fichier >> liste.pilotes[i].nCourses;
        fichier >> liste.pilotes[i].nVictoires;
        fichier.ignore();

        // liste des annees
        string line;
        getline(fichier, line);

        int position = 0, compteur = 0;     
        while ((position + 4) > line.length()) {    // doesn't work, nothing gets in the array
            line.substr(position, 4) =
              liste.pilotes[i].annees.liste[compteur];
            position += 5;
            compteur++;
        }
        liste.pilotes[i].annees.n = compteur;

        // liste des constructeurs automobiles
        getline(fichier, line);
        //????
    }

variable names are in french as required by my class but it shouldn't be too hard to understand(pays = country, annees = years, constructeur = sponsor). Please keep in mind it's a first year programming class, so I am not allowed to use advanced concept like objects and vectors...

Thanks in advance for your input

Adam
  • 121
  • 1
  • 10
  • 1
    I would like to suggest you tu create function to retrieve the data from the lines: one for the name, one for the next line data and so on...learn to estructure your programs is a good practice and will help you a lot on thinking about problems. Divide and conquer!! – Netwave Nov 08 '15 at 16:34
  • try to separate your problems into smaller portions, one function should do only one thing, you end up with many functions, but the more versatile your program gets. – OlimilOops Nov 08 '15 at 16:39
  • I'll keep that in mind, but I am not allowed to create more than 3 functions in that assignment and they're all taken... – Adam Nov 08 '15 at 16:59

0 Answers0