Archived

This topic is now archived and is closed to further replies.

drb2k2

Easy Arrays question

Recommended Posts

drb2k2    122
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. Cheers drb2k2
int 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[i] << endl;
	}

	infile.close();
 

Share this post


Link to post
Share on other sites
drb2k2    122
No okay that isn't quite working as I hoped, here is the new code


string* 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?
cheers
DRb2k2

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]

Share this post


Link to post
Share on other sites
FlamePixel    122
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[ i ]   


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.


[edited by - FlamePixel on March 21, 2004 9:07:38 AM]

Share this post


Link to post
Share on other sites
drb2k2    122
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[i] << endl;
cout << "i is " << i << endl;
}

infile.close();

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.
Cheers
DRb2k2

Share this post


Link to post
Share on other sites
drb2k2    122
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 do

char jim[22];
infile.getline(jim,22);
for(int i=0; i {
std::getline(infile,p_MeshHolder, ''\n'');
cout << "::" << p_MeshHolder[i] << endl;
cout << "i is " << i << endl;
}

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.
Cheers
DRb2k2

Share this post


Link to post
Share on other sites
Thunder_Hawk    314
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
______________________________________________________________

Share this post


Link to post
Share on other sites