Reading data from file into two arrays

#include <iostream>
#include <fstream>
using namespace std;


int main()
{
const int ARRAY_SIZE = 1000; // Array size
int accountNumbers[ARRAY_SIZE]; // Array with 1000 elements
double accountBalances[ARRAY_SIZE]; // Loop counter variable
int count; // Input file stream object
ifstream inputFile;

// Open the file.
inputFile.open("Final_Random_Accounts_Unsorted.txt.rtf");

// Read the numbers from the file into the array
while (count < ARRAY_SIZE && inputFile >> accountNumbers[count] >> accountBalances[count]) {
count++;
}

inputFile.close();

// Display the read data
cout << "The bank account numbers are: ";
for (count = 0; count < ARRAY_SIZE; count++) {
cout << accountNumbers[count] << " " << endl;
}

cout << "The bank balances are: ";
for (count = 0; count < ARRAY_SIZE; count++) {
cout << accountBalances[count] << " " << endl;
}
}

So this is what I started with, not to familiar with reading files honestly.

My results when I run gives me out of mostly 0's for bank accounts results and few large random numbers in there, the results for bank balances gives me extremely odd numbers, ex: 2.435325e-310

The file I am attempting to read has 1000 "bank account numbers" along with there balances.
Consider using a struct instead of 2 arrays as with sorting etc the relationship b/w a/c numbers and a/c balances might break quite easily. Also std::vector over C-style arrays:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <vector>
#include <fstream>

struct BankDetails
{
    int m_accountNum;
    double m_accountBal;
};
std::ostream& operator << (std::ostream& os, const BankDetails& bd)
// for printing
{
    os << bd.m_accountNum << " : " << bd.m_accountBal << "\n";
}

int main()
{
    std::ifstream inFile{"D:\\test.txt"};
    std::vector<BankDetails> accounts{};
    while (inFile)
    {
        int accountNum{}; double accountBal{};
        inFile >> accountNum >> accountBal;
        if(inFile)accounts.emplace_back(BankDetails{accountNum, accountBal});
        //check file is still good to avoid spurious entries at the end
        //http://en.cppreference.com/w/cpp/container/vector/emplace_back
    }
    for (const auto& elem : accounts)std::cout << elem;
}
/*
SAMPLE FILE
1 100.10
2 200.20
3 300.30

SAMPLE OUTPUT
1 : 100.1
2 : 200.2
3 : 300.3
*/
Last edited on
@Chris213, I assume that your input file is a simple ASCII text file; i.e. you could open it with a simple text editor like notepad. The name of your file suggests otherwise.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <fstream>

int main()
{
    const int ARRAY_SIZE = 1000; // Array size
    int accountNumbers[ARRAY_SIZE] = {0} ; // initialize to all zeroes
    double accountBalances[ARRAY_SIZE] = {0} ; // initialize to all zeroes

    // Open the file. important: make sure that the file is a
    // plain text file; not one in the rich text format (.rtf)
    std::ifstream inputFile( "Final_Random_Accounts_Unsorted.txt" );

    // verify that the file was opened
    if( !inputFile.is_open() ) {

        std::cerr << "failed to open input file\n" ;
        return 1 ;
    }

    int count = 0 ; // actual count of items read; initialise to zero

    // Read the numbers from the file into the array
    while( count < ARRAY_SIZE && inputFile >> accountNumbers[count] >> accountBalances[count] ) {

            ++count;
    }
    // count now holds the actual number of items read; do not modify it after this point

    inputFile.close();

    // Display the read data
    std::cout << "The bank account numbers and balances are: ";
    for( int i = 0; i < count ; ++i ) { // for each pair of values up to count

        // print the values
        std::cout << accountNumbers[i] << ' ' << accountBalances[i] << '\n' ;
    }
}
Last edited on
Topic archived. No new replies allowed.