podomunro
podomunro

Reputation: 673

Reading various amount of values into array from text file

I'm trying to read some data from a .txt file into some variables, which will then be passed into a class object. The first few values from the text file are working fine (firstNames, surname, dob, accountNo), however the bankNumbers are causing trouble. An account holder can have between 0 and 5 accounts, so some may have 3, or 0, or 5. At the minute, it reads in the next 5 values, so if my first user has only 3 accounts, my program will read in the accountNo and surname as the 4th and 5th values of the array. How do i make the program only read the number of numbers that are there? Here is an example text file:

548161 Bloggs Joe 01-01-1970 1567 1824 2041
378941 Smith John 25-12-1985 
123085 Claus Santa 30-05-1910 7829 2398 4890 1473 4392
318945 Obama Barack 14-02-1965 4382 3944

And here is my code:

int main()
{
    ifstream accountsFile;
    string surname, firstNames, dob;
    int accountNo, bankNumbers[5];

    accountsFile.open("Accounts.txt", ifstream::in);

    int i = 0;
    while (!accountsFile.eof())
    {

        accountsFile >> accountNo >> surname >> firstNames >> dob;

        for (int i = 0; i < 5; i++)
        {
            accountsFile >> bankNumbers[i];
        }

        accounts[i] = Account(accountNo, surname, firstNames,  dob, bankNumbers);
        i++;
    }

    accountsFile.close();

    system("pause");
    return 0;
}

Upvotes: 0

Views: 766

Answers (1)

amallard
amallard

Reputation: 1229

change all the variables to strings instead. that way you can just use the getline() method to grab each line (which will be one whole account)

then split the input line into a string array, and fill in the variables accordingly for the account number, surname, first name, then date of birth.

then for the bank numbers, fill them in individually using the array starting at index 4 ( which will be the first bank number) until you reach the end of the line string array.

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{
    ifstream accountsFile;
    string surname, firstName, dob;
    string accountNo, bankNumbers[5];

    accountsFile.open("Accounts.txt", ios::in);

    string input;
    int accountsIndex = 0;

    if (accountsFile)
    {
        getline(accountsFile, input);

        while (accountsFile)
        {
            string line[9];
            int lineIndex = 0;

            for (int x = 0; x < input.length(); x++) {
                line[lineIndex] += input[x];
                if (input[x] == ' ') {
                    lineIndex++;
                }
            }

            accountNo = line[0];
            surname = line[1];
            firstName = line[2];
            dob = line[3];

            for (int x = 4; x < (sizeof(line) / sizeof(*line)); x++)
            {
                bankNumbers[x - 4] = line[x];
            }

            accounts[accountsIndex] = Account(accountNo, surname, firstName, dob, bankNumbers);
            accountsIndex++;

            getline(accountsFile, input);
        }

        accountsFile.close();
    }

    system("pause");
    return 0;

}

Upvotes: 1

Related Questions