|
-
March 23rd, 2008, 05:43 AM
#1
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??
-
March 23rd, 2008, 09:11 AM
#2
Re: Array Problem
Use a std::vector of std::string
-
March 23rd, 2008, 07:24 PM
#3
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
-
March 23rd, 2008, 09:50 PM
#4
Re: Array Problem
 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.
-
March 25th, 2008, 12:10 AM
#5
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
-
March 25th, 2008, 12:35 AM
#6
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.
-
March 25th, 2008, 02:25 PM
#7
Re: Array Problem
 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
-
March 29th, 2008, 11:12 AM
#8
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 =]
-
March 29th, 2008, 05:43 PM
#9
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
-
March 29th, 2008, 05:54 PM
#10
Re: Array Problem
 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
}
-
March 29th, 2008, 06:07 PM
#11
Re: Array Problem
Confused But ill give it ago =]
Learning :
C++
More PHP
More VB
Learnt :
HTML
CSS
PHP
VB
-
March 29th, 2008, 07:00 PM
#12
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|