Thread: variable.item or array[named] possible?

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    3

    variable.item or array[named] possible?

    K, I am use to PHP programing so this is why I am a little confused, I have sat down and layed out what I want to program, its a small game that is texted based and I am sure I will laught at it later on, but I need to start somewhere. Anyways what I want if for a variable or array to have other variables inside it, basicly an array but instead of having numbers for the indexs I want names. here is example.

    player1[lifes] = 100
    player1[score] = 200
    player1[homeland] = "eldar"

    something like that, I think when playing with VB a while back (sorry if VB is a bad word here, i promis not to use it anymore) there was something you could do like

    player1.lifes = 100
    player1.score = 100
    player1.homeland = "eldar"

    I know I can use standard variables, but this looks SO much cleaner and makes ALOT more sence. However I am not sure how to do it, This is one way I come up with, but I feel that it is Geto Rigged and that there is probebly a better way out there, please let me know if there is.

    int player1[10];
    int life;
    life = 1;
    player1[life]=100;
    cout << player1[life];
    cin.get();

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    You would want to use structs aka classes.
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    struct player
    {
      
      int lives;
      int score;
      string homeland;
      
    };
    
    int main()
    {
      
      player player1;
      player1.lives = 3;
      player2.score = 0;
      player.homeland = "Eldar";
      cout<<player1.lives;
      //etc...
      
      return 0;
      
    }
    Woop?

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    3
    OMG, that is perfect, that is what I was looking for, tahnk you so much.

  4. #4
    ---
    Join Date
    May 2004
    Posts
    1,379
    or you could use enum
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    typedef enum{eldar,something,something_else}LAND;
    
    struct player
    {
      
      int lives;
      int score;
      LAND homeland;
      
    };
    
    
    int main()
    {
      
      player player1;
      player1.lives = 3;
      
      player1.homeland = eldar;
      cout<<player1.lives;
      //etc...
      
      return 0;
      
    }

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    I think when playing with VB a while back (sorry if VB is a bad word here, i promis not to use it anymore) there was something you could do like

    player1.lifes = 100
    player1.score = 100
    player1.homeland = "eldar"
    You can do the same thing in php, although the notation isn't quite as simple:

    Code:
    <html>
    <body>
    
    <?php
    class Player
    {
        var $lives;
        var $score;
        var $homeland;
    }
    
    $player1 = new Player();
    $player1->lives=100;
    $player1->score=100;
    $player1->homeland="eldar";
    
    echo("Lives: " . $player1->lives);
    ?>
    </body>
    </html>
    Last edited by 7stud; 02-14-2005 at 12:31 AM.

  6. #6
    Registered User
    Join Date
    Feb 2005
    Posts
    3
    THank you for that PokerPlayer (7Stud), I have not really used Classes before but I am starting to see how awesome they are. Thank you again.

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    THank you for that PokerPlayer (7Stud).


    I have not really used Classes before but I am starting to see how awesome they are.
    ...and have very complex capabilities in C++. php and javascript(which shares the VB syntax) provide basic classes, although javascript classes are more powerful than they might first appear, and php5 has extended class features.
    Last edited by 7stud; 02-14-2005 at 10:33 AM.

Popular pages Recent additions subscribe to a feed