Input from txt file to arrays

I have a text file with a line like:

James Dean         10 Automotive      27010.43

      

and I need to read this file and put each of the 4 into arrays.

char nameArray[MAX][NAME_MAX];
int yearArray[MAX];
char departmentArray[MAX][DEP_MAX];
double payArray[MAX];


while(i < MAX && infile) {

        infile.getline(nameArray[i], 20);
        infile >> yearArray[i];
        infile.getline(departmentArray[i], 15);
        infile >> payArray[i];

        cout << nameArray[i] << "  " << yearArray[i] << "  " << departmentArray[i] << "  " << fixed << setprecision(2) << payArray[i] << endl;
        i++;
    }

      

The code is cut out to give you an idea of ​​what I am trying to do, but when I run this I get something like:

James Dean         -858993460    -92559631349317830000000000000000000000000000
000000000000000000.00

      

Thanks for the help.

==== Edit ============================================ ===================================

I switched from getline to get, thanks for that. I have to use get and not → because some of the lines I read are more than just "James Dean", they are up to 20 char long ... ex: "William K. Woodward" is another one,

So, if I just use get, then it reads the first line ok, but then I get the same messed up text for the second line.

Here is the code:

    infile.get(nameArray[i], 20);
    infile >> yearArray[i];
    infile.get(departmentArray[i], 15);
    infile >> payArray[i];

      

+1


source to share


2 answers


The functions getline

take an input stream and a string to write. Thus, the two calls getline

are read on two lines. Your input mechanism is not working. Either use getline

or the stream extract operator (i.e. >>

), but not both.

If you plan on using it getline

, you need to parse the string (which is actually one line of input) into tokes and then store them in arrays with the appropriate character set. The second and fourth tokens are numbers, so you will need to convert them from string to int

or double

.

Approach operator >>

:



string name, surname;
int year;
double pay;
while (infile) {
    infile >> name >> surname >> year >> department >> pay;
    namearray[ i ] = name + " " + surname;
    // ...
    payarray[ i ] = pay;
    ++i;
}

      

Approach getline

:

string line;
while (getline(infile, line)) {
    parse(line, tokens);
    namearray[ i ] = token[ 0 ] + " " + token[ 1 ];
    // ...
    payarray[ i ] = strTodouble(token[ 4 ]);
    ++i;   
}

// parse definition
void parse(string line, vector<string>& token) {
    // roll your own
}

double strToDouble(string s) {
   // ...
}

      

+2


source


I don't see where you define infile, but I will assume it is istl. In this case, you should use it the same way you would use cin for input.

Why are you doing getline ()? This function will only stop at '\ n' char or EOF char. Means you start reading int after the end of the line, some random data.



Correct me if I'm wrong, but is there 20 or 19 characters on this first line (James Dean) before number (10)?

0


source







All Articles