Reading in from txt file and making a dynamic array

The first reading in of values to set up an array works just fine but when i try to do the second one it seems to skipthe while loop and give me 0's for my array
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
#include<iostream>	
#include<cmath>
#include<string>
#include<fstream>
#include<vector>
using namespace std;

//======================================================================




int main () { 

//FILLING DATA ARRAYS 
// ask user for dat file
 
	ifstream dat;
	do {
		cout << "Input filename where the raw data exist-->";
		char filename [50];
		cin.getline(filename,50);
		dat.open(filename);
		if (!dat.is_open())
		cout << "File Does Not Exist!" << endl ;
	} while (!dat.is_open());

	double (*data)[3]= new double [15][3];

	int x = 0 ;
	double  a,b,c,d,e,f;

	while (	dat >> a >> b >> c){

		data[x][0] = a;
		data[x][1] = b;
		data[x][2] = c;

		cout << data[x][0] << endl; // to check
		x++;
	}
	
	dat.close();
// fill values from giant text fill that I typed by hand!! 

	ifstream table;
	do {
		cout << "Input filename where the data table exist-->";
		char file [50];
		cin.getline(file,50);
		table.open(file);
			if (!table.is_open())
			cout << "File Does Not Exist!" << endl ;
	} while (!table.is_open());


	double (*values)[6] = new double[500][6]; 
				//array from 1967 table values are:
			      // jf , ji,l1,l2,f2,f4 
	x = 0 	;
// it will cout here 

	while (table >> a >> b >> c >> d >> e >> f){

		values [x][0] = a;
		values [x][1] = b;		
		values [x][2] = c;
		values [x][3] = d;
		values [x][4] = e;
		values [x][5] = f;
	cout << "loop" << endl;   // this is not printing ??!?
	cout << values [x][2] << endl;
		x++;
	}		
//and it will cout here as well 

	table.close();

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

cout << values [x][0] << endl; // returns all 0's 
} 


return (0);
}
Last edited on
At a guess, the format of the second file doesn't match the variables you're getting input for and the condition on line 63 evaluates to false.
double (*data)[3]= new double [15][3];
is this really OK !!!
same in line 57.

i just have this feeling that this should make some problems.

maybe the problem isn't here, but i wonder about it.
Cire :

the format of the file has a bunch of numbers that I typed out separated by spaces
and the file looks like :

#Jf , Ji , L1 , L2 , F2 , F4
1 0 1 1 0.70711 0
1 1 1 1 -0.35355 0
1 1 1 2 -1.06066 0
1 1 2 2 -0.35355 0
1 2 1 1 0.07071 0
1 2 1 2 0.47434 0
1 2 2 2 0.35355 0
1 3 2 2 -0.10102 0
1 3 2 3 0.37796 0
1 3 3 3 0.53033 0
1 4 3 3 -0.17678 0
2 0 2 2 -0.59761 -1.06904
2 1 1 1 0.41833 0
2 1 1 2 -0.93541 0
2 1 2 2 -0.29881 0.7127
2 2 1 1 -0.41833 0

ECT......

Therefore i believe the format of the file is fine

Rechard3:

The double (*data)[3]= new double [15][3]; line is fine im sure seeing how it works for the the first one....


something funny is going on here and i cant figure out what exactly!
#Jf , Ji , L1 , L2 , F2 , F4


If this line is in the file, the first extraction operation will fail.
That line ( #Jf , Ji , L1 , L2 , F2 , F4 ) makes no difference when I remove it.
Last edited on
I found the issue!!

I was within the text file. There was a line with a space between the negative sign and the number ie. 2 1 2 2 - 0.29881 0.7127

I also took out the top #Jf , Ji , L1 , L2 , F2 , F4 line as well just incase
Last edited on
I agree with cire. You should remove that line from the text file. The first extraction operation into a double would fail, which would cause line 63 to evaluate to false.
Topic archived. No new replies allowed.