CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: Array Problem

  1. #1
    Join Date
    Mar 2008
    Posts
    4

    Array Problem

    Ok well first off, i am coding a game in c++, its text based and im coding it to get experince in c++ as i am relevently new To Coding in c++.

    Ok , so my problem is i want to make an inventory where when i click buy weapons it stores the weapon/ item anme inside an array and i can then call this array for when im attacking monsters or make an if statement to say if the array[1] = dagger, so on so forth then my strength increases..


    any idea??

  2. #2
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    Re: Array Problem

    Use a std::vector of std::string

  3. #3
    Join Date
    Feb 2008
    Posts
    79

    Re: Array Problem

    I would use object oriented programming instead of relying on strings.

    Create a 'Weapon' class...
    Create a 'Dagger' sub class of Weapon...

    give your character a vector of type 'Weapon'...

    then, in battle, you can delegate the weapons ability to the dagger object, instead of the battle object needing to know of special dagger cases...

    Good luck!
    Cheers,
    Jon

  4. #4
    Join Date
    Oct 2004
    Posts
    296

    Re: Array Problem

    Quote Originally Posted by Dragonreaper
    Ok well first off, i am coding a game in c++, its text based and im coding it to get experince in c++ as i am relevently new To Coding in c++.

    Ok , so my problem is i want to make an inventory where when i click buy weapons it stores the weapon/ item anme inside an array and i can then call this array for when im attacking monsters or make an if statement to say if the array[1] = dagger, so on so forth then my strength increases..


    any idea??
    So what's the problem? Normally, you post your code, and then you ask a question about why it doesn't work.
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
    	string weapons[5];
    	
    	weapons[0] = "sword";
    	weapons[1] = "dagger";
    	
    	if(weapons[1]=="dagger")
    	{
    		cout<<"I have a dagger.";
    	}
    	
           return 0;
    }
    Last edited by 7stud; March 23rd, 2008 at 09:58 PM.

  5. #5
    Join Date
    Feb 2008
    Posts
    27

    Re: Array Problem

    I would say you should do it in a few arrays... (keep in mind im by no means experienced) one array for the names of weapons, and another with an attack power. then you just would add the index of a weapon to a third array (say "myweapons")... just my 2c

  6. #6
    Join Date
    Jan 2008
    Posts
    10

    Re: Array Problem

    If classes are a little over your head right now, i believe you could try and use structs.

    struct weapons
    {
    stats, names, whatever
    };

    weapons melee[10]
    weapons ranged[10]
    etc.

  7. #7
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Array Problem

    Quote Originally Posted by holykiwi
    If classes are a little over your head right now, i believe you could try and use structs.

    struct weapons
    {
    stats, names, whatever
    };

    weapons melee[10]
    weapons ranged[10]
    etc.
    What is the difference between a struct and a class? In C++ they are exactly the same, except for the default access permissions.

    Viggy

  8. #8
    Join Date
    Mar 2008
    Posts
    4

    Re: Array Problem

    Ahh Thanks =]

    I am suggesting to recode my game with class and things.. as my code is starting to get complicated lol

    Well thanks for everything you have helped loads =]

  9. #9
    Join Date
    Mar 2008
    Posts
    4

    Re: Array Problem

    Was Just Wondering..

    Classes..

    am i doing this right?

    class Player {

    public:
    string name;
    int hp;
    int str;
    int mana;
    int exp;
    int n-exp;
    int gold;
    int level;

    }

    int main() {

    Player;

    cin >> Player.name;

    return 0;
    }
    Learning :
    C++
    More PHP
    More VB
    Learnt :
    HTML
    CSS
    PHP
    VB

  10. #10
    Join Date
    Jun 2006
    Location
    M31
    Posts
    885

    Re: Array Problem

    Quote Originally Posted by Dragonreaper
    Was Just Wondering..
    am i doing this right?
    No. You're working with non-static members; you need to work on an instance of Player.

    Example:
    Code:
    struct MyObj
    {
        int i;
    };
    
    void Foo()
    {
        MyObj obj = {}; //"obj" is a stack-based instance of a MyObj object. Note that it's a POD-type.
    
        ++obj.i; //"i" in "obj" is now 1
    }

  11. #11
    Join Date
    Mar 2008
    Posts
    4

    Re: Array Problem

    Confused But ill give it ago =]
    Learning :
    C++
    More PHP
    More VB
    Learnt :
    HTML
    CSS
    PHP
    VB

  12. #12
    Join Date
    May 2007
    Posts
    811

    Re: Array Problem

    Dragonreaper, in your case:

    Code:
    class Player {
    public:
       string name;
       int hp;
       int str;
       int mana;
       int exp;
       int n-exp;
       int gold;
       int level;
    }
    
    int main() {
    
       Player myPlayer;
    
       cin >> myPlayer.name;
    
       return 0;
    }
    Compare my code snippet with yours and notice the difference.
    Also, don't you agree that it's easier to read my post then yours. Please use code tags, if you do not know how, there is FAQ link at the top of this page, please read it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured