Thread: Variable Names based on Variable Values

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    21

    Variable Names based on Variable Values

    greetings,

    i wasnt to sure what to name the subject but what im trying to do is basically display variables based on the value of another variable. consider this...

    char Name1[10] = "Test1";
    char Name2[10] = "Test2";
    int Value = 2;

    now rather than doing

    switch(Value)
    {
    case 1:
    cout << Name1 << endl;
    break;

    case 2:
    cout << Name2 << endl;
    }

    could i instead do somethign along the lines of


    cout << NameValue << endl;

    ie. appending the value of "Value" in this case "2" to that of Name and thus printing the value of Name2 ?

    im trying to do this with structures so i have enemies1, enemies2, enemies3, etc, etc based on what level player is on. Therefore rather than using switch and cases if I coudl just append the value of what level they are on to "enemies" and thus pulling the correct data.

    I have dont this before in PHP but not sure if its possible with C++.

    I hope you can understand what i mean.

    Thanks in advance for any advice given.

  2. #2
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    Store the Names in a two dimensional array.

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
    	char Names[2][10] = {"Test1", "Test2"};
    	int i;
    
    	i = 0;
    	cout << Names[i] << endl;
    	++i;
    	cout << Names[i] << endl;
    
    	return 0;
    }
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  3. #3
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    No, you can't do it like that. There are a couple of ways to do this, the easiest - although probably not the best - would be to make them into a two-dimensional array like so:
    Code:
    char Name[1][10] = "Test1";
    char Name[2][10] = "Test2";
    int Value = 2;
    
    cout<<Name[value]<<endl;

  4. #4
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    Another option is to use a map
    Code:
    #include <iostream>
    #include <string>
    #include <map>
    
    using namespace std;
    
    int main()
    {
      map<int, string> tbl;
      string Name1 = "Hi", Name2 = "there!";
    
      tbl[1] = Name1;
      tbl[2] = Name2;
    
      cout<< tbl[2] <<endl;
    }
    If you're exceptionally daring you can use the preprocessor to paste things together, this isn't a recommended solution though. :-)
    Code:
    #include <iostream>
    #include <string>
    
    #define call_name(x) (Name##x)
    
    using namespace std;
    
    int main()
    {
      string Name1 = "Hi";
      string Name2 = "there!";
    
      cout<< call_name(2) <<endl;
    }
    *Cela*

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    21
    Thanks for your replies and I follow what you are saying however im looking to do this with structure names.

    for example.

    Code:
    struct enemy
    {
      char name[50];
      int whatever;
    }
    
    enemy level1[] =
    {
      {"Dog", 10},
      {"Cat", 20}
    }
    
    enemy level2[] =
    {
      {"Chicken", 30},
      {"Horse", 40}
    }
    
    int main()
    {
      int CurrentLevel = 2;
      cout << levelCurrentLevel[0].name << endl;
    }
    so im using that value of CurrentLevel to pull the corresponding data from the correct Structure.

    so im not sure how i can incorporate your two-dimensional array into this ?

    Thank You

  6. #6
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>so im not sure how i can incorporate your two-dimensional array into this ?
    Something like this would be simple enough. :-)
    Code:
    #include <iostream>
    
    using namespace std;
    
    struct enemy
    {
      char name[50];
      int whatever;
    };
    
    enemy level1[] =
    {
      {"Dog", 10},
      {"Cat", 20}
    };
    
    enemy level2[] =
    {
      {"Chicken", 30},
      {"Horse", 40}
    };
    
    static enemy *level[] = {
      0, // No level0?
      level1,
      level2,
    };
    
    int main()
    {
      int CurrentLevel = 2;
      cout << level[CurrentLevel][0].name << endl;
    }
    *Cela*

  7. #7
    Registered User
    Join Date
    Jan 2003
    Posts
    21
    Thank you very much

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Controlling variable values by printing on file
    By p3rry in forum C Programming
    Replies: 8
    Last Post: 12-17-2008, 10:09 AM
  2. Storing int and double type values under one variable name
    By Thanuja91 in forum C++ Programming
    Replies: 10
    Last Post: 10-30-2007, 04:15 AM
  3. Replies: 5
    Last Post: 02-09-2003, 10:03 AM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. creating a filename based on a variable
    By Waldo2k2 in forum C++ Programming
    Replies: 3
    Last Post: 05-22-2002, 05:27 PM