Over Filling Arrays

I have a loop that should fill an array with data from a text file until the end of file. when it fills the array it duplicates the last data point and adds it to the end of the array.

the loop looks like :

1
2
3
4
5
6
7
8
9
10
11
	while (! dat.eof()){

		dat >>  a >>  b >>  c;

		theta [x] = a ;
		wthe [x] = b;
		errwth [x] = c;
		cout << theta[x] << endl;//check to see if filled right
		x++;
	}

Here is what the screen output looks like:

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
dhilchie@mars  9:58am ->./a.out 
Input filename where the raw data exist-->dat.dat
17.5
37.5
55
71
90
109
125
142.5
162.5
180
180
Input filename where the data table exist-->ef.txt
9
7
7
 Below are the subtracted values (data - model)  of wtheta's
0.106068
0.130888
-0.0496249
0.0031718
-0.0483844
-0.0231538
0.0186203
0.028337
0.0718316
0.0935172
dhilchie@mars  9:59am -> 


its a small part of a larger code that has that same style loop again :

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136

float math (float f2, float f4,float f22, float f42, float theta)
{

//this function does the w(theta) calculation 

	float wtheta = 0 ;
	

	wtheta = 1 + f2*f22*1/2*(3* pow(cos(theta),2) *-1 ) 
			+ f4*f42* 1/8*(35*pow(cos(theta),4)
			- 30 *pow(cos(theta),4) + 3);


	

return (wtheta);
}




int main () 
{

//FILLING DATA ARRAYS 
// ask user for dat file
 
	start:
	cout << "Input filename where the raw data exist-->";
	char filename [50];
	ifstream dat;
	cin.getline(filename,50);
	dat.open(filename);

// if file isnt there report error 

	if (!dat.is_open()){
		cout << "File Does Not Exist!" << endl ;
	goto start;
	}

// read from file  and fill arrays 
	
	float theta[20], wthe[20], errwth[20];	
	int x = 0; 



	float a,b,c,d,e,f;

	while (! dat.eof()){

		dat >>  a >>  b >>  c;

		theta [x] = a ;
		wthe [x] = b;
		errwth [x] = c;
		cout << theta[x] << endl;//check to see if filled right
		x++;
	}

	dat.close();

// SECOND ARRAY FILLING 
// fill arrays for testing models 

	beg:
	cout << "Input filename where the data table exist-->";
	char file [50];
	ifstream table;
	cin.getline(file,50);
	table.open(file);

// if file isnt there report error 

	if (!table.is_open()){
		cout << "File Does Not Exist!" << endl ;
	goto beg;
	}

	x = 0 	;
	float ji [10], jm [10], jf[10], l1[10], l2[10] ;
	float f2[10] , f4[10] ;

	while (! table.eof()){

		table >>  a >>  b >>  c >> d >> e >> f;

		jf [x] = a;
		ji [x] = b;
		l1 [x] = c;
		l2 [x] = d;
		f2 [x] = e;
		f4 [x] = f;
		
		cout << jf [x] << endl;
	
		x++;
	}		


 /*
// testing calls not in a subroutine to check if calc is correct 
	float mwtheta ;
	mwtheta = 1 + f2[0]*f2[1]*1/2*(3* pow(cos(theta[0]),2) *-1 ) 
			+ f4[0]*f4[1]* 1/8*(35*pow(cos(theta[0]),4)
			- 30 *pow(cos(theta[0]),4) + 3);


	cout << mwtheta << endl;
*/


// call subroutine to do the model wtheta calcs 


	cout << " Below are the subtracted values (data - model)  of wthetas" << endl;

	for ( x = 0 ; x < 10 ; x++ ){

		float ans;	
		ans = math (f2[0],f4[0],f2[1],f4[1], theta[x] );

//		cout << ans << endl;

		float final = wthe[x] - ans ;
	

		cout << final << endl;
	}
	


return 0 ;
}
while (! dat.eof()){
Problem is here.
Do not ever use eof() as loop condition. Results will probably be off-by-one.
Use: while(dat >> a >> b >> c) { or similar when handling input or check stream state manually after each input operation.
Thanks that fixed it
Topic archived. No new replies allowed.