converting to a constant

Started by
1 comment, last by Motwner 22 years, 3 months ago
I have been working on a simple program that reads a number from a text file and then implements that number into an array bracket... example: File>>chOut; scanf(chOut,"%d",&tri); TRI*TRIptr[tri]; The problem is that ''tri'' must be a constant, right? Please show me how to convert tri to a const. Thank ya... -Later
Advertisement

What you''re trying to do can''t be done. Not like this.
That is, if I understood correctly what you''re trying to do...

However, something like this might work;

int tricount = atoi ( string_from_file );TRI *triangles = new TRI[tricount];// okay, you could use //   TRI *triangles = (TRI*)malloc(tricount*sizeof(TRI));TRI *tri_number_0 = triangles[0];TRI *tri_number_1 = triangles[1]; 


Or possibly

std::vector<trI*> triangles(tricount);TRI *tri_number_0 = triangles[0]; 


Preferably the latter. It at least fails properly if (when) you screw up...
~~~ "'impossible' is a word in the dictonary of fools" --Napoleon
Odd... you''re mixing c++ file operations (File>>chOut) with c file operations (scanf(chOut,"%d",&tri). I have no clue why :o).

  FILE *in;int tri;TRI *TRIptr;in = fopen("trifile.txt","r");fscanf(in,"%d",&tri);//Here you can eitherTRIptr = new TRI[tri];//And when done, remember to delete!!delete TRIptr;//or//Allocate memory - must include something to use this.. but I forgot the .h file.. might try memory.h or malloc.h or something similarTRIptr = (TRI*)malloc(tri*sizeof(TRI));//Free memoryfree(TRIptr);  


Personally I''d stick with the "new" and "delete" operators for memory allocating/deallocating.

Billy - BillyB@mrsnj.com

This topic is closed to new replies.

Advertisement