creating an array of pointers

Nov 4, 2010 at 10:44pm
So this is a program that reads numbers from a file, computes the total number of numbers in the file, then creates an array that holds those values. Then, it creates an array of pointers that holds the pointers to all the elements of the array. One thing i don't understand is how is the variable num equal to zero. I get the error "cannot allocate an array of constant size 0". num is supposed to be 9 (because that is how many elements i have in my donations file).

#include <iostream>
#include <fstream>
using namespace std;

int main()
{
int num = 0;
ifstream inputFile;
double number;
inputFile.open("donations.dat");
if(!inputFile)
cout << "Error" << endl;
else
{
while(inputFile >> number)
{
num++;
}
//creating an array of pointers
cout << num << endl;
double *ptr;
ptr = new double[num];
for (int count = 0; count < num; count++)
ptr[count] = (number - count);
double *Array[num]; // how is num equal to zero????????

for (int numba = 0; numba < num; numba++)
Array[numba] = &ptr[numba];
}
inputFile.close();
return 0;
}
Last edited on Nov 4, 2010 at 10:45pm
Nov 5, 2010 at 2:48pm
Have you stepped thru the code with a debugger? You really should do that first.
Topic archived. No new replies allowed.