#### Archived

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

# converting to a constant

This topic is 6188 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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

##### Share on other sites

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

##### Share on other sites
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

1. 1
2. 2
Rutin
19
3. 3
khawk
18
4. 4
5. 5
A4L
12

• 12
• 16
• 26
• 10
• 44
• ### Forum Statistics

• Total Topics
633767
• Total Posts
3013738
×