Thread: newb question: dynamic variable names

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    28

    newb question: dynamic variable names

    I need to have a loop that goes an undefined amont of times, and each time creates a new variable with a different name, i.e.

    variable(x) = 54
    variable(x) = 13
    ect.
    where x will increase by one each time.

    The problem is that I dont know how to use a variable in the name of the variable itself when declaring or accessing it.

    Of course any suggestions for other methods that will have the same functionality would also be helpful.

  2. #2
    verbose cat
    Join Date
    Jun 2003
    Posts
    209
    As long as all the variables will use the same data type (like all ints as in your example), then you can use vectors.
    abachler: "A great programmer never stops optimizing a piece of code until it consists of nothing but preprocessor directives and comments "

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Use a dynamic array, std::vector would be appropriate.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    What you are looking for is scripting language functionality. JessyCat is right, if you can have them all be the same variable type you can use vectors.

  5. #5
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Use a vector container.
    Code:
    #include <iostream>
    #include <vector>
    
    int main() {
       std::vector<int> myVariables;
       int currVar;
    
       std::cout << "Enter numbers seperated by spaces. Press ENTER to stop." << std::endl;
       do {
          std::cin >> currVar;   // Not bothering with validity checking
          myVariables.push_back(currVar);
       } while(std::cin.peek() != '\n');
       std::cin.ignore();
    
       std::cout << "You entered the numbers: ";
       for(std::vector<int>::iterator i = myVariables.begin(); i < myVariables.end(); i++)
           std::cout << *i << ' ';
        
       std::cin.get();
       return 0;
    }
    EDIT: Note to self, take a little less time on an example, next time.
    Sent from my iPadŽ

  6. #6
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    There is no way to alter variable names in source code from source code. You can't run a loop to alter a variable name you have declared.

    So if you want 20 strings, you either use an array or a container.

    It is a good thing you cannot alter source code from source code and compile it.
    You can open your source code as a text file and then insert what you want, and then run the compiler on it, but that's not what you are doing here.

  7. #7
    Registered User
    Join Date
    Jul 2006
    Posts
    28
    Thanks for the suggestion, I will try it out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Variable names starting with _
    By edesign in forum C Programming
    Replies: 3
    Last Post: 06-05-2009, 03:47 AM
  2. random variable names
    By NeMewSys in forum C Programming
    Replies: 7
    Last Post: 06-12-2008, 11:26 AM
  3. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  4. Variable question I can't find answer to
    By joelmon in forum C++ Programming
    Replies: 3
    Last Post: 02-12-2002, 04:11 AM
  5. Variable Allocation in a simple operating system
    By awkeller in forum C Programming
    Replies: 1
    Last Post: 12-08-2001, 02:26 PM