Trying to store data from file into 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
#include <iostream>
#include <fstream>
using namespace std;


int main()
{
    const int ARRAY_SIZE = 1000;            // Array size
    double 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("FinalNumbers.txt");
    
    // 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 (int count = 0; count < ARRAY_SIZE; count++) {
        cout << accountNumbers[count] << "\n" << accountBalances[count] << " " << endl;
    }
    
}


The code is printing out random numbers for first 20 or so numbers then carries on to print the right data skipping the last 20 or so numbers because the array runs out of space.

What I am aiming to do is make sure I have all the data stored properly then store the account numbers in ascending order with the proper balance paired to the account number...
Last edited on
Line 11: What do you think the initial value of count is? Hint: It's an uninitialized variable. It contains garbage.

Line 18: You're reading into a random place in the array because count contains garbage.

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Thank you and thank you!
Topic archived. No new replies allowed.