populating array using pointer

I'm pretty new still to c++ so bear with me, I'm given a .cpp file (for homework), this file populates an array from a file, that contains only numbers, using the standard method of populating an array but I'm supposed to modify it so that the array gets populated using a pointer, pretty simple right? Not so for me, I think I'm still confused on the concept of pointers. Here's what I have so far...
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
  int main()
{
    const int MAX_SIZE = 10;

    float double_array[MAX_SIZE];
    float *pt_double_array = double_array[0];
     
    ifstream in;
    in.open("numbers.txt",ios::in);
  
    if(in.fail){
        cerr<<"File could not be pened."<<endl;
        system("PAUSE");
        exit(1);
    }
    else
        cout<<"File read successfully"<<endl;


    while(!in.eof()){ 
    
       for(int i = 0; i < MAX_SIZE; i++)
	   {
	   	in >> pt_double_array;    //this might be the area i'm going
	   	in >> ws;                  //wrong 
                pt_double_array++;
        
	   }
  
    }
	
	delete pt_double_array;
	pt_double_array = NULL;
	
    system("PAUSE");
    return 0;
}
Last edited on
mistake1: float *pt_double_array = double_array[0]; It should be float *pt_double_array = double_array; (And why your double array is of float type?)

mistake 2: your loop is messed up. It can read more values that your file contains and can overwrite first values if file is larger than MAX_SIZ values

mistake 3: you are overwriting pointer value, not the value it points to.

All you need is that instead of lines 20-30:
1
2
while(pt_double_array != double_array + MAX_SIZE && in >> *pt_double_array)
    ++pt_double_array;
Topic archived. No new replies allowed.