Jump to content

C++ how to get file values into an array

Pachuca

I have a text file with two columns one for account number and the other for account balance. I need to write a function that will read the existing account information for 15 customers from that text file into a pair of parallel arrays. The program should call a function to read data into the arrays until an end-of-file condition is encountered. The function should then return the number of records read. The function I'm using is called selectBalance and this is what I have so far, but I can't figure out how to get the array to take every other value from the text file so that it reads only the account number first than another array should do the same except read the account balance only. Any help would be appreciated, thank you. 

[code]
using namespace std;

const int ARRAY_SIZE = 15;

void mainMenu(char* menuSelection);
void selectBalance(ifstream &, int [], int);

 

 

int main() {

    ifstream infile;
    ofstream outfile;
    int array1[ARRAY_SIZE], array2[ARRAY_SIZE];

    infile.open("accounts.txt");
    if (infile.fail()) {
        cerr << "Accounts file failed to open.\n";
        abort();
    }

do {
        mainMenu(&menuSelection);
        if (menuSelection == 'W' || menuSelection == 'w')
            selectWithdrawl();
        else if (menuSelection == 'D' || menuSelection == 'd')
            selectDeposit();
        else if (menuSelection == 'B' || menuSelection == 'b')
            selectBalance(infile, array1, ARRAY_SIZE);
        else if (menuSelection == 'Q' || menuSelection == 'q')
            selectQuit();
        else
            mainMenu(&menuSelection);
    } while (menuSelection != 'q' && menuSelection != 'Q');

    return 0;
}

 

void selectBalance(ifstream &in, int arrayA[], int elem) {

    cout << "Please enter the account number from the list below to display balance:\n";
    for (int i = 0; i <= ARRAY_SIZE; i=i++) {
        in >> arrayA;
        cout << arrayA << "        " << i << "\n";
    }
} 


[/code]

Link to comment
Share on other sites

Link to post
Share on other sites

place text file into spread sheet then resize the x column widths fit your text data

now array use y as define for account number to account balance

(y1x1=y7x64). datatexthere=numbervaluehere

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×