Trying to fill dynamic array from txt file

I am trying to fill a dynamic array from a text file but I keep getting errors this is my first attempt at doing something with dynamic arrays so im not too sure on what is going wrong!
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
#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;
	data = new double [15][3];
	int x;
	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][2] << endl; // to check
		x++;
	}
	

return (0);
}


the errors are (line numbers are incorrect):

dhilchie@mars 1:23pm ->gpp vbays.cpp
vbays.cpp: In function ‘int main()’:
vbays.cpp:62: error: cannot convert ‘double (*)[3]’ to ‘double*’ in assignment
vbays.cpp:68: error: invalid types ‘double[int]’ for array subscript
vbays.cpp:69: error: invalid types ‘double[int]’ for array subscript
vbays.cpp:70: error: invalid types ‘double[int]’ for array subscript
dhilchie@mars 1:26pm ->
You have #include<vector> ; use std::vector<>?

If you must use arrays,

1
2
3
4
5
6
7
//double * data;
//data = new double [15][3];

// type of data is 'pointer to array of 3 double'
// an array, where each element is of type 'double[3]' is being allocated
// new returns a pointer to the first element; ie a pointer to double[3] 
double (*data)[3] = new double [15][3];
i work from a template and the include <vector> was just there
Data is a pointer and can represent one-dimensional array. The dynamic memory allocation reserves two-dimensional array.

Another error is that you do not initialize x.


Why do you include cmath, string, and vector? Your code does not use them. You should use them, rather than plain arrays, for "production code".
Topic archived. No new replies allowed.