Click to See Complete Forum and Search --> : Array Problem
Dragonreaper
March 23rd, 2008, 05:43 AM
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??
MikeAThon
March 23rd, 2008, 09:11 AM
Use a std::vector of std::string
jon.borchardt
March 23rd, 2008, 07:24 PM
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!
7stud
March 23rd, 2008, 09:50 PM
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.
#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;
}
aosmith
March 25th, 2008, 12:10 AM
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
holykiwi
March 25th, 2008, 12:35 AM
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.
MrViggy
March 25th, 2008, 02:25 PM
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
Dragonreaper
March 29th, 2008, 11:12 AM
Ahh Thanks =]
I am suggesting to recode my game with class and things.. as my code is starting to get complicated lol :p
Well thanks for everything you have helped loads =]
Dragonreaper
March 29th, 2008, 05:43 PM
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;
}
Plasmator
March 29th, 2008, 05:54 PM
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:
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
}
Dragonreaper
March 29th, 2008, 06:07 PM
Confused But ill give it ago =]
STLDude
March 29th, 2008, 07:00 PM
Dragonreaper, in your case:
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.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.