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
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
Perhaps an associative array mapping some strings to some values?
http://en.wikipedia.org/wiki/Associative_array
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
@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.
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
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
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
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.