Thread: dynamically declaring variables

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    7

    dynamically declaring variables

    if i have two int variables, say x and y, how would i declare a variable named "whatever<x><y>" except replacing x and y with their current values?

    if this isnt possible what would be a suitable substitute

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Perhaps an associative array mapping some strings to some values?

    http://en.wikipedia.org/wiki/Associative_array

  3. #3
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    Code:
    #include<iostream>
    using namespace std;
    
    class Whatever
    {
      public:  
      stuff( );
    };
    
    int main()
    {
        int x=0,
            y=0;
    
        cout << "Enter a number: " ;
        cin >> x ;
    
        cout << "\nEnter another number: " ;
        cin >> y ;
    
        Whatever myWhatever[x][y] ;
    
        //as an added extra bonus:
        for(int i=0; i<x; i++)
           for(int j=0; j<y; j++)
    
               cout << myWhatever[i][j].stuff() ;
    
    return 0 ;
    }
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    903
    @The Brain: I guess what smartalco wanted was *naming* variable with the pattern 'name<x><y>' which is impossible I think. Besides, your code cannot even compile, you would need to use dynamic memory allocation to allocate an array with a variable size.

  5. #5
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    oops..

    Code:
    #include<iostream>
    using namespace std;
    
    class Whatever
    {
      public:  
      stuff( );
    };
    
    int main()
    {
        int x=0,
            y=0;
    
        cout << "Enter a number: " ;
        cin >> x ;
    
        cout << "\nEnter another number: " ;
        cin >> y ;
    
        Whatever **myWhatever = new Whatever*[x] ;
        for(int i=0; i<x; i++)
            myWhatever[i] = new Whatever[y] ;
    
        //as an added extra bonus:
        for(int i=0; i<x; i++)
           for(int j=0; j<y; j++)
    
               cout << myWhatever[i][j]->stuff() ;
    
    return 0 ;
    }
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  6. #6
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Yes, the code itself cannot reflect some variable factor. That would make for some very strange possibilities, wouldn't it? But yes, Tonto's suggestion is the best idea if you are looking to associate some variable with two values. Say you wanted to simulate integers with dynamic names:
    Code:
    std::srand(std::time(NULL));
    int x, y;
    int val;
    std::stringstream name;
    std:map<std::string,int> variables;
    for(int loop = 0; loop < num_of_vars; loop++)
    {
       x = std::rand();
       y = std::rand();
       name << "whatever<" << x << "><" << y << '>';
       cin >> val;
       variables[name.str()] = val;
       name.str("");
    }
    //assuming we know whatever<3><65> was created...
    cout << "Here is your variable whatever<3><65>: " << (variables["whatever<3><65>"]) << endl;
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  7. #7
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    The Brain forgot to delete the array. I posted some allocation and deallocation functions for 2d dynamic arrays a while ago here if you're interested. Deleting the array is just as important as assigning it in the first place! A lecturer told us a story before about some aircraft auto pilot of some kind in which the programmer (s?) didn't delete the memory they allocated. It literally ran out of it and crashed! Pilot obviously ejected, but millions of dollars were waisted! BTW, shouldn't it be "whatever<y><x>" technically

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    In C++ you would use a vector of vectors or other suitable container for dynamically creating variables.

    Of course, I think the OP wants to name the variables dynamically, which isn't possible. Use of a vector is probably an appropriate alternative.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating local variables in a program?
    By fl0at in forum C Programming
    Replies: 5
    Last Post: 01-04-2008, 07:34 PM
  2. Remotely Creating Variables
    By Rajin in forum C++ Programming
    Replies: 1
    Last Post: 04-26-2005, 11:20 PM
  3. Declaring an variable number of variables
    By Decrypt in forum C++ Programming
    Replies: 8
    Last Post: 02-27-2005, 04:46 PM
  4. hwnd and variables in them
    By underthesun in forum Windows Programming
    Replies: 6
    Last Post: 01-16-2005, 06:39 PM
  5. functions to return 2 variables?
    By tim in forum C Programming
    Replies: 5
    Last Post: 02-18-2002, 02:39 PM