Jump to content
  • Advertisement

Archived

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

Motwner

converting to a constant

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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 either

TRIptr = 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 similar

TRIptr = (TRI*)malloc(tri*sizeof(TRI));
//Free memory

free(TRIptr);


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

Billy - BillyB@mrsnj.com

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!