I am relatively new to C++ programming (about half a year of C programming learning and 2 months of C++), so my question might seem (or be) stupid, but I've been searching the internet for awhile and also trying to figure out whats going on.
I wrote a code that makes 2d (and later another to 3d) dynamic array which is working fine, but when I put it in to separate function there is a problem when I try to put the data (using cin) to this array.
My code to make 2d array:
double tab** = new double*[x];
for(int i=0; i<x; i++){
tab[i] = new double[y];
}
And then to put the data into the array:
double tmc;
for(int i=0; i<x; i++){
for(int j=0; j<y; j++){
cout<<"Type the value of : "<<i+1<<", "<<j+1<<endl;
my_fun_input(&tmc);
tab2d[i][j]=tmc;
}
}
my_fun_input is my function that looks like:
void my_fun_input(double* number){
cin>>*number;
while(cin.good()==false){
cout<<"Error! Bad value."<<endl;
cin.clear();
cin.ignore();
cin>>*number;
}}
And the problem is that when I use it directly it' s working, but when I move the first piece of code that is creating the 2d array to separate function like this:
void table(double** tab, int x, int y){
tab = new double*[x];
for(int i=0; i<x; i++){
tab[i] = new double[y];
}}
I am using this function like this:
double** tab2d=NULL;
int x, y; //size obtained from user
table(tab2d, x, y);
After using this function the code to put data into works for first element and then the program is crushing. What can cause this? Thanks for help in advance.
PS. Sorry for my poor english.
std::vectorto create a 2D array, instead of fiddling with raw pointers (by that I meandouble** tab). If you really want to learn C++, try learning to do thing the C++ way. Here is a an article to get you started with using 2D vectors: techiedelight.com/vector-of-vector-cpp