What is wrong with this function?

// /////// //
void GetData(int(IDs)[], float(Scores)[][4], int &i) {            
//GIVEN: One blank ARRAY, one blank 2D ARRAY, & a index variable.
//TASK: Function retrieves data from open stream into multiple functions. Stores ID into one structure and all of the student's test grades into another (two-dimensional) structure.
//RETURNS: Index variable & two ARRAYS now holding relevant information.
	ifstream text("data3.txt", ios::in);        //Opendata3.txt  for read-in through the <fstream.h> class.                 

	do                                          //Do while loop is used to place data
	{                                           //from open stream into storage.
		text>>IDs[i]; 
		text>>Scores[i][i];
		text>>Scores[i][i+1];
		text>>Scores[i][i+2];
		text>>Scores[i][i+3];
		i++;
	}	while(IDs[i-1]>0);
	i-=2;
	text.close();                               //Closes open stream.
}

data format in 'data3.txt' consists of records, first line being the student's 4 digit id number and the second being his four test grades. i.e:
2148
90.0 85.0 85.3 42.9
4248
24.2 66.0 89.5 96.0
and so on with about 45 records total.

after executing the function and continuing on (in a different function) to output that stored data this function is supposed to read-in, i get this:
(the problem outlined in red)

Test scores retreived from 'data3.txt':
StudentID Test 1: Test 2: Test 3: Test 4:
#1118647091 66.8 90.9 87.7 65.6
#1117546086 99.5 88.8 55.9 94.5
#8703 88.8 77.6 66.6 89.9
#1110835200 90.4 77.3 72.2 68.8
#1113718784 77.2 77.8 88.3 83.2
#1107977830 55.9 89.9 82.2 84.6
#1113482854 98.8 93.5 94.1 90.2
#1108 89.9 33.5 76.4 67.1
#1117611622 66.5 55.7 55.8 44.9
#1117336371 88.9 88.0 81.4 82.4
#1117415014 66.7 56.8 71.2 65.9
#1116654797 55.5 66.6 66.2 62.1
#6713 67.3 77.8 65.9 87.2
#1114741146 99.3 92.2 87.8 90.3
#1119210701 66.1 62.6 54.1 45.2
#1119918490 55.4 34.4 23.4 77.1
#1117480550 77.4 66.8 75.5 75.2
#8718 80.3 78.9 71.4 62.2
#3319 77.8 77.9 72.4 80.1
#9820 99.9 89.9 99.9 94.6
#1921 66.7 77.1 76.6 79.3
#8822 88.2 82.2 78.9 68.3
#4523 98.3 89.3 78.4 99.4
#7324 77.9 34.7 78.9 78.4
#5425 56.6 45.6 34.5 23.8
#6526 66.7 56.2 78.9 55.9
#9927 92.3 92.3 94.3 80.2
#6228 90.9 88.3 88.8 91.1
#2829 76.8 87.4 87.1 79.3
#5830 55.8 89.9 78.2 90.3
#6831 66.8 90.9 58.4 69.4
#6732 69.9 78.7 50.2 67.5
#7733 77.9 71.2 76.7 75.3
#5234 82.2 81.9 72.6 75.6
#2335 98.3 87.3 93.7 89.2
#1936 77.3 66.3 74.8 50.6
#6737 66.2 67.3 72.1 45.6
#8538 88.8 59.2 67.7 90.5
#5639 55.5 66.6 54.5 59.9
#8840 77.8 98.2 88.3 78.5
#8641 88.8 67.8 86.6 78.2
#1042 45.5 56.5 34.6 55.6
#6343 78.7 76.6 77.2 71.4
#9244 60.4 90.9 96.3 77.7

*all the test data is correct and correct in regards to where in the list it should be. however the student id's (in areas outlined in red) are completely off-base. why is it reading in data half-correctly for the first bit of the file and then completely correctly for the second bit?

Indexing into the second dimension of Scores is wrong, causing boundry overflow and scribblins all over memory. See below.

text>>Scores[i][0];
text>>Scores[i][1];
text>>Scores[i][2];
text>>Scores[i][3];

that took care of it. thanks; however, it also throws the order off. the beginning of the data file
2148
90.0 85.0 85.3 42.9
4248
24.2 66.0 89.5 96.0
3231
22.0 46.8 42.8 90.1 42.4
(and so on)
when read into Scores gives this as output

#2148 90.0 85.0 85.3 42.9
#4248 66.0 89.5 96.0 22.0
#3231 46.8 42.8 90.1 42.4

first line is right however at the beginning of the second element row outputted it seems that the read-in missed one. as the remainder of the output goes along that same basis, effected by the missing element from the beginning.

there is something else wrong then. Post your newest code.

there is something else wrong then. Post your newest code.

ifstream text("data3.txt", ios::in);        //Opendata3.txt  for read-in through the <fstream.h> class.                 

	do                                          //Do while loop is used to place data
	{                                           //from open stream into storage.
		text>>IDs[i]; 
		text>>Scores[i][0];
		text>>Scores[i][1];
		text>>Scores[i][2];
		text>>Scores[i][3];
		i++;
	}	while(IDs[i-1]>0);
	i-=2;
	text.close();                               //Closes open stream.
}

data3.txt:

6901
66.8 90.9 87.7 65.6
9802
99.5 88.8 55.9 94.5
8703
88.8 77.6 66.6 89.9
9704
90.4 77.3 72.2 68.8
7705
77.2 77.8 88.3 83.2
5806
55.9 89.9 82.2 84.6
9907
98.8 93.5 94.1 90.2
1108
89.9 33.5 76.4 67.1
3809
66.5 55.7 55.8 44.9
2210
88.9 88.0 81.4 82.4
6511
66.7 56.8 71.2 65.9
5612
55.5 66.6 66.2 62.1
6713
67.3 77.8 65.9 87.2
9914
99.3 92.2 87.8 90.3
6615
66.1 62.6 54.1 45.2
5316
55.4 34.4 23.4 77.1
7617
77.4 66.8 75.5 75.2
8718
80.3 78.9 71.4 62.2
3319
77.8 77.9 72.4 80.1
9820
99.9 89.9 99.9 94.6
1921
66.7 77.1 76.6 79.3
8822
88.2 82.2 78.9 68.3
4523
98.3 89.3 78.4 99.4
7324
77.9 34.7 78.9 78.4
5425
56.6 45.6 34.5 23.8
6526
66.7 56.2 78.9 55.9
9927
92.3 92.3 94.3 80.2
6228
90.9 88.3 88.8 91.1
2829
76.8 87.4 87.1 79.3
5830
55.8 89.9 78.2 90.3
6831
66.8 90.9 58.4 69.4
6732
69.9 78.7 50.2 67.5
7733
77.9 71.2 76.7 75.3
5234
82.2 81.9 72.6 75.6
2335
98.3 87.3 93.7 89.2
1936
77.3 66.3 74.8 50.6
6737
66.2 67.3 72.1 45.6
8538
88.8 59.2 67.7 90.5
5639
55.5 66.6 54.5 59.9
8840
77.8 98.2 88.3 78.5
8641
88.8 67.8 86.6 78.2
1042
45.5 56.5 34.6 55.6
6343
78.7 76.6 77.2 71.4
9244
60.4 90.9 96.3 77.7
-999

-999 being sentinel

output (printing out what we just stored):

Test scores retreived from 'data3.txt':
StudentID Test 1: Test 2: Test 3: Test 4:
#6901 66.8 90.9 87.7 65.6
#9802 88.8 55.9 94.5 88.8
#8703 66.6 89.9 90.4 77.3
#9704 68.8 77.2 77.8 88.3
#7705 55.9 89.9 82.2 84.6
#5806 93.5 94.1 90.2 89.9
#9907 76.4 67.1 66.5 55.7
#1108 44.9 88.9 88.0 81.4
#3809 66.7 56.8 71.2 65.9
#2210 66.6 66.2 62.1 67.3
#6511 65.9 87.2 99.3 92.2
#5612 90.3 66.1 62.6 54.1
#6713 55.4 34.4 23.4 77.1
#9914 66.8 75.5 75.2 80.3
#6615 71.4 62.2 77.8 77.9
#5316 80.1 99.9 89.9 99.9
#7617 66.7 77.1 76.6 79.3
#8718 82.2 78.9 68.3 98.3
#3319 78.4 99.4 77.9 34.7
#9820 78.4 56.6 45.6 34.5
#1921 66.7 56.2 78.9 55.9
#8822 92.3 94.3 80.2 90.9
#4523 88.8 91.1 76.8 87.4
#7324 79.3 55.8 89.9 78.2
#5425 66.8 90.9 58.4 69.4
#6526 78.7 50.2 67.5 77.9
#9927 76.7 75.3 82.2 81.9
#6228 75.6 98.3 87.3 93.7
#2829 77.3 66.3 74.8 50.6
#5830 67.3 72.1 45.6 88.8
#6831 67.7 90.5 55.5 66.6
#6732 59.9 77.8 98.2 88.3
#7733 88.8 67.8 86.6 78.2
#5234 56.5 34.6 55.6 78.7
#2335 77.2 71.4 60.4 90.9
#1936 77.7-107374176.0-107374176.0-107374176.0
#6737 -107374176.0-107374176.0-107374176.0-107374176.0
#8538 -107374176.0-107374176.0-107374176.0-107374176.0
#5639 -107374176.0-107374176.0-107374176.0-107374176.0
#8840 -107374176.0-107374176.0-107374176.0-107374176.0
#8641 -107374176.0-107374176.0 0.0 0.0
#1042 0.0 0.0 0.0 0.0
#6343 0.0 0.0 0.0 0.0
#9244 0.0 0.0 0.0 0.0

I don't get the problem you have. I changed the do loop to a while loop and deleted the line that said i -=2;

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

long IDs[255] = {0};
float Scores[255][4] = {0};
int main()
{
   ifstream text("data3.txt");  
    if(!text.is_open())
    {
        cout << "Error opening file\n";
        return 1;
    }
    int i = 0;
	while(i < 255 && text>>IDs[i] && IDs[i] > 0)
	{  
		text>>Scores[i][0];
		text>>Scores[i][1];
		text>>Scores[i][2];
		text>>Scores[i][3];
		i++;
	}
	text.close();                               //Closes open stream.

    for(int k = 0; k < i; k++)
    {
        cout << IDs[k] << "   ";
        for(int j = 0; j < 4; j++)
            cout  << Scores[k][j] << "  ";
        cout << "\n";
    }
}

produces this output

66.8 90.9 87.7 65.6
99.5 88.8 55.9 94.5
88.8 77.6 66.6 89.9
90.4 77.3 72.2 68.8
77.2 77.8 88.3 83.2
55.9 89.9 82.2 84.6
98.8 93.5 94.1 90.2
89.9 33.5 76.4 67.1
66.5 55.7 55.8 44.9
88.9 88 81.4 82.4
66.7 56.8 71.2 65.9
55.5 66.6 66.2 62.1
67.3 77.8 65.9 87.2
99.3 92.2 87.8 90.3
66.1 62.6 54.1 45.2
55.4 34.4 23.4 77.1
77.4 66.8 75.5 75.2
80.3 78.9 71.4 62.2
77.8 77.9 72.4 80.1
99.9 89.9 99.9 94.6
66.7 77.1 76.6 79.3
88.2 82.2 78.9 68.3
98.3 89.3 78.4 99.4
77.9 34.7 78.9 78.4
56.6 45.6 34.5 23.8
66.7 56.2 78.9 55.9
92.3 92.3 94.3 80.2
90.9 88.3 88.8 91.1
76.8 87.4 87.1 79.3
55.8 89.9 78.2 90.3
66.8 90.9 58.4 69.4
69.9 78.7 50.2 67.5
77.9 71.2 76.7 75.3
82.2 81.9 72.6 75.6
98.3 87.3 93.7 89.2
77.3 66.3 74.8 50.6
66.2 67.3 72.1 45.6
88.8 59.2 67.7 90.5
55.5 66.6 54.5 59.9
77.8 98.2 88.3 78.5
88.8 67.8 86.6 78.2
45.5 56.5 34.6 55.6
any key to continue . . .

there is still something i don't understand. even using your exact input code my code still doesn't input correctly. look at my above post and note the pattern of difference from data3.txt to the output. it's quite residual but i just can't figure out what is affecting it to make it do that and not output like yours.

scratch that. i didn't realize i was still using the

[i+1]
[i+2]
[i+3]
as part of the output. that was what was causing me the output trouble. thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.