0

The problem is that I can't get the values to save into the indexed points of the array. When I attempt to print the 2D array, it prints in the correct format, but I just get crazy numbers instead of the ones from the text file. This leads me to believe that my function to fill the array is not working properly, but I'm having trouble diagnosing where the problem is.

Here is my function to fill the array.

void fill2dArray(int array[][4], int size, int& numberUsed)
{
ifstream recordsFile;
int index1, index2;

recordsFile.open("data11.txt");

while ((index1 < size) && (!recordsFile.eof()))
{
    for (index1 = 0; index1 < size; index1 = index1 + 1)
        for(index2 = 0; index2 < 4; index2 = index2 + 1)
            recordsFile >> array[index1][index2];

}
numberUsed = index1;
recordsFile.close();
}

Here is my function to print the array.

void print2dArray(const int array[][4], int numberUsed)
{
int index1, index2;

for (index1 = 0; index1 < numberUsed; index1 = index1 + 1)
{
    for (index2 = 0; index2 < 4; index2 = index2 + 1)
        cout << array[index1][index2] << " ";

    cout << endl;
}
}

Here is the globals/prototypes

const int NOP = 25; //Max number of players in the records file

void fill2dArray(int array[][4], int size, int& numberUsed);
void print2dArray(const int array[][4], int numberUsed);

Here is the main

int main()
{
int records[NOP][4], numberUsed;

fill2dArray(records, NOP, numberUsed);

print2dArray(records, numberUsed);


return 0;
}

And the text file (stored in the same folder as the program)

1 2 1 1
2 3 2 3
3 2 3 1
4 0 3 4
5 2 1 3
6 5 2 5
7 2 4 2
8 4 1 0
9 0 2 3
10 1 4 3
11 2 3 1
12 3 5 6
13 2 3 5
14 2 1 0
15 2 1 4
16 7 3 5
17 9 3 2
18 6 2 1
19 3 2 0
20 1 0 0
21 0 0 0
22 1 2 5
23 2 4 2
24 6 2 7
25 6 2 4
tshepang
  • 10,772
  • 21
  • 84
  • 127
Victor
  • 57
  • 2
  • 8

1 Answers1

1

At the beginning of the fill2dArray, you define index1 but don't assign any value to it, so it's undefined, it may be less than size or more than size.

Simply assign 0 to index1 variable, at the beginning.

prajmus
  • 2,995
  • 3
  • 31
  • 38
  • I tried this but still getting uninitialized values in the output. I've rewritten the function to where I manually input values into the array, and it will print fine. That leads me to believe there is a problem with the text file I'm using to test with. The file is located in a folder off of my desktop co-located with the program. I'm going to try and see if there is anything weird going on within the .txt file itself. – Victor Jun 24 '14 at 16:12
  • @Victor I ran your code and it worked for me, so feel free to ask more if you find something new – prajmus Jun 24 '14 at 16:15
  • I figured out the problem. The text file was named data11.txt... which I guess the program was reading it as data11.txt.txt. Sigh.. Thanks for the help everyone! – Victor Jun 24 '14 at 16:15