Sunday, August 2, 2009

How can I write a program in c++ that can create its own variables?

I want to write a program in c++ that can declare new variables for iteself, and thus be only limited by a computer's memory. I am fairly new to programming in general, so I was wondering if there was a simple answer or a bit of code I could past in.


Thanks! :)

How can I write a program in c++ that can create its own variables?
Create a dynamic array, with three dimensions, or three seperate dynamic arrays. Use one array to store variable names as a string format, and one to denote variable type, use string for human readable assignment, or use a char array for symbol codes. Then finally, use the third array to store the value, preferably in string form.





Create functions to manipulate these variables, expanding and contracting the arrays when needed.





Good luck!
Reply:O'Neill's answer might be a little overpowered for what you need to do.


In C++ memory is dynamically allocated using the new operator (although, the C malloc() function is still available). So if you want to create an int, you would do it like so:


int *pint = new int;


Yes, you will still need to create the variable pint, but that won't be the int, it will be a pointer to an int. The int would be *pint.


When you are no longer using that int, you should deallocate the memory with delete pint.
Reply:#include %26lt;iostream%26gt;


using namespace std;


int main()


{


cout %26lt;%26lt; "Enter number of variables to create" %26lt;%26lt; endl;


int NumberOfVariables = 0;


cin %26gt;%26gt; NumberOfVariables;


int *VariablesHere = new int[NumberOfVariables];


}

pistil

No comments:

Post a Comment