   Easy Arrays question
  Hi all,I am fairly new to C++ from JAVA and am having problems initialising arrays. Basically I am reading a file from disk. A number read from the disk is used to initialise the size of an array. However when I try and run the program it complains about the value not being const. If I change the array to a pointer to array it still complains about expecting a constant expression. Does anyone know how to get around this situation.Cheersdrb2k2int main() {   int g_NumberOfMeshes = 0;int g_NumberOfObjects = 0;int g_NumberOfAreas = 0;    ifstream infile;   infile.open("File");    if (!infile) {   cout << "Unable to open file";while(1){}   exit(1); // terminate with error    }infile >> g_NumberOfMeshes >> g_NumberOfObjects >> g_NumberOfAreas;string* g_MeshHolder[g_NumberOfMeshes]; for(int i=0; i<2;i++){std::getline(infile,g_MeshHolder, ''\n'');cout << g_MeshHolder << endl;}infile.close(); </pre>   </i>  
  Sorry everyone, I was simply not using the NEW keyword.DRb2k2 ;  No okay that isn't quite working as I hoped, here is the new codestring* p_MeshHolder = NULL;p_MeshHolder = new string[g_NumberOfMeshes]; for(int i=0;i{std::getline(infile,*(p_MeshHolder + i), '\n');cout << "::" << *(p_MeshHolder + i) << endl;}infile.close(); but it isn't doing what I want. The first line that is printed is perfect. But the next line is empty. The next line off file is definately there though. Is it an error with my pointer arithmatic?cheersDRb2k2   hmmmmmmmmm the forum is altering the code for some reason. the for loop is i less than number of Meshes, i plus plus[edited by - drb2k2 on March 21, 2004 8:47:23 AM] [edited by - drb2k2 on March 21, 2004 8:48:18 AM] [edited by - drb2k2 on March 21, 2004 8:49:00 AM] [edited by - drb2k2 on March 21, 2004 8:49:29 AM];  Well first of all, if string is std::string, all you need to create your array is:string g_MeshHolder[g_NumberOfMeshes];    You don't want an array of pointers to strings, as indicated by your original post:string* g_MeshHolder[g_NumberOfMeshes];    in you original post.  In your second post, you are making the entire process just to complicated.Also, you needn't do all that pointer arithmetic when you can use the array bracket operator.  It's as simple as:g_MeshHolder   </pre>   <br><br>EDIT: The forum seems to have a problem with i inside array brackets.  It thinks I'm tring to make something italic.  Anyway, just take the spaces out in between the i and the brackets.<br>   <br><br><SPAN CLASS=editedby>[edited by - FlamePixel on March 21, 2004 9:07:38 AM]</SPAN>;  No that doesn''t work.If I use:std::string g_MeshHolder[g_NumberOfMeshes];  it says ''expected constant expression''but taking your advice on the pointer arithmatic i now have:string* p_MeshHolder = NULL;p_MeshHolder = new string[g_NumberOfMeshes];//std::string g_MeshHolder[g_NumberOfMeshes]; for(int i=0; i{std::getline(infile,p_MeshHolder, ''\n'');cout << "::" << p_MeshHolder << endl;<br>cout << "i is " << i << endl;<br>}<br><br>infile.close();<br> </pre> <br>This is still not working though. The first line that is printed is blank, the second is the filename that should have been in the first. I''m really confused. <br>Cheers<br>DRb2k2  </i>  ;  okay, for some reason it is not advancing the read marker past the end of the third line from the file i.e. the NumberOfAreas value. So the first line is always empty.  If i dochar jim[22];infile.getline(jim,22);for(int i=0; i{std::getline(infile,p_MeshHolder, ''\n'');cout << "::" << p_MeshHolder << endl;<br>cout << "i is " << i << endl;<br>}<br> </pre> <br>it works perfectly, but isn''t too elegant. Does anyone know how to advance the read cursor. I''m going around in circles with the MSDN help file for ifstream, I can''t find its methods.<br>Cheers<br>DRb2k2  </i>  ;  Getline has a gotcha in that it leaves the terminating character in the buffer.  Try adding a call to ignore just before the getline call.  It''s been a while since I''ve seen this sort of code, but I think that''s the problem.   ______________________________________________________________The Phoenix shall arise from the ashes...   ThunderHawk -- ¦þMySite______________________________________________________________ 