Transferring data from file to arrays

I'm trying to transfer data from a file to an array.

I'm sure the problem is right in front of my face but I've been working for 8hrs or so and I just can't figure out what's wrong.

Here is my code:
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
void readFile(int scores[], int number)
{
    int column;
    
    string filename;
    
    ifstream data;
    
    cout<< "Which file would you like to open? ";
    cin>>filename;
    
    data.open(filename);
    
    if(!data)
    {
        cout<< "Error opening file!" << endl;
    }
    else
    {
        for(column = 0; column < number; column++)
        {
            data >> scores[column];
            cout<< scores[column];
        }
        data.close();
        cout<< "I have read in the scores from the file " << filename << "." << endl;
    }
    
    cout<< endl <<"Please choose another option from the menu: ";
}

The output is just the default data I put in the array. The error message doesn't pop up either. Any help would be appreciated. Thanks.
Please post a small sample of your input file. Also please show how scores was defined and how you are calling this function.

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
int scores[number] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

    do {
        
        cin>>choice;
        cout<<endl;
        
        while((choice < 1) || (choice >8))
        {
            cout<<endl<<"Please enter a number between 1 and 8.";
            cin>>choice;
        }
        
        switch(choice)
        {
            case 1:
                readUser(scores, number);
                break;
            case 2:
                readFile(scores, number);
                break;
            case 3:
                printScores(scores, number);
                break;
            case 4:
                printHighest(scores, number);
                break;
            case 5:
                printLowest(scores, number);
                break;
            case 6:
                printAverage(scores, number);
                break;
            case 7:
                printOne(scores, number);
                break;
        }
    }while(choice != 8);


input file:
47
89
65
55
62
95
87
75
100
79
Line 22 attempts to read an integer from the file. If the file does not contain a valid integer, this array value will be unchanged.

You can test that the read was successful, before proceeding, like this:
1
2
3
4
5
6
7
8
9
        int column = 0;
        int val;
        while (data >> val && column < number)
        {
            scores[column] = val;
            cout << scores[column] << " ";
            column++;
        }
        cout << column << " values were read into the array" << endl;
I know the file contains only integers so that cant be the problem. Does anybody know whats going wrong here?
You could check data.good() at various points in the program, to see whether the input stream is in a valid state. Add some cout messages for debugging purposes.

Or use a debugger to inspect the variables and flow of control during execution.
Last edited on
The original code worked fine for me, using the values you provided. I called the function with the following main:
1
2
3
4
5
6
7
8
9
#include <fstream>
#include <iostream>
#include <string>

int main()
{
   int numbers[10];
   readFile(numbers, 10);
}

I did however add a space to the printout so I could see the different values.
cout<< scores[column] << " ";
Topic archived. No new replies allowed.