Help with Classes and Arrays

Hello all,
I am a beginner in programming, so having lots of trouble getting started with and assignment. Any help would be greatly appreciated. This is the assignment.

There will be a file called "Weapons.txt" which contains 3 pieces of information about weapons.
There will be the following data:
string name; // contains the name of the weapon
int hands; // contains how many hands required to use the weapon
int attackpower; //contains the attack power the weapon has per hit.

**this next variable is not in the file and will be generated by constructor**
double attackspeed; //this requires a bit of explaining:
//lets say the attack power of the weapon is 10. IF the weapon has an attackspeed
//of 2, that means he does 2 attacks per second, so his damage per second is 20.
//if his attackspeed was .5, he attacks once every 2 seconds so his damage per second is 5 (average).
The attackspeed will be generated randomly in the constructor from 0.1 to 2.5


This means the class Weapon will have 4 variables (name, hands, attackpower, attackspeed);

There will also be a class called Hero with only three variables:
int str; // the strength of the hero
string lefthand; //name of weapon on left hand
string righthand; //name of weapon on right hand
This str will also be generated randomly by the constructor using rand()%10+1;

The hero has the obvious... two hands! You should determine the best weapon combination
for the hero for him to deal the highest damage per second (dps).

**There is a restriction though... You can only equipt weapons you are strong enough to equipt.
This means that if your str is 5, the combined attackpower of your weapons equipt should be 5 or less.
This really makes things tougher.


To begin, you need to infile all the weapons into the array of size 10 correctly.
Once all the weapons are set up, you should use bubble sort to sort the weapons by your choice of
either attackspeed, attackpower, or maybe even dps; (use a function named sortWeapons)

Display all the weapons information.
*At this point you should have found the weapon(s) to equipt*
Display the Hero's information (his attack strength and the name of the weapon on each hand).
IF the hero is using a two handed weapon, then the same name of the weapon is on both the left and right hand.
IF he is using two one handed weapons, then there should be two different names.
Display these names along with the combined attack damage per second.


**be aware that the two handed weapons are stronger than one handed, but you can
equit two one handed weapons.
***You cannot equipt two of the same weapon
** The classes should have all set and get functions, constructor, variables should be private **
**The main should have an array of 10 weapons and a hero. it should have functions as you find needed.

Alright show us some code, Ill help you a bit:

First we need a weapon class, easy enough:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Weapon
{
    std::string name;
    int hands = 0,attackPower = 0;
    double attackSpeed = 0.0;

    /*Constructor*/
public:
     Weapon(std::string &inName,int &inHands,int &inAttackPower,double &inAttackSpeed):name(inName),hands(inHands),attackPower(inAttackPower),attackSpeed(inAttackSpeed){};

std::string getName()
{
     return name;
}
void setName(std::string setName)
{
    name = setName;
}
/*The rest of Get and Sets here*/
};


That should give you a good start here, try writing some code until you absolutely get stuck, google problems and if you cant get anything then post here again.

Last edited on
This is what I have so far. Don't know where to go from here.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <string>
#include <cmath>
#include <fstream>
#include <cstdlib>


using namespace std;

class Weapon
{
private:
	string name;
	int hands;
	int attackpower;
	double attackspeed;

public:
	Weapon()
	{
	attackspeed = (rand()%25)*.1;
	}
	double getAts(){return attackspeed;}
};



class Hero
{
private:
	int str;
	string lefthand;
	string righthand;

public:
	Hero()
	{
	str = rand()%10+1;
	}
	int getStr(){return str;}
};

int main()
{

	string array[10];
	short loop = 0;
	string line;

	ifstream myfile ("Weapons.txt");
	if (myfile.is_open())
	{
		while (! myfile.eof())
		{
			getline (myfile,line);
			array[loop] = line;
			cout << array[loop] << endl;
			loop++;
		}
		myfile.close();
	}
	else cout <<"Unable to open file";
	
	return 0;
}
Alright a couple of things.


You have blank constructors and no get or sets for the Hero and Weapon class. How are you going to change variables such as hands or attack power? For every weapon those should be different.


Another thing, you dont instantiate your Hero or Weapon objects in the main function

1
2
3
4
5
6
7
int main()
{
     Weapon ak47;
     Weapon m4a1;
     Hero mgarza;
     Hero jakee;
}



I also thought you only wanted 10 weapons to go into the file and you will go out of bounds in the array if you dont include this.


1
2
3
4
5
6
7
8
9
		while (! myfile.eof() && loop < 11)
		{
			getline (myfile,line);
			array[loop] = line;
			cout << array[loop] << endl;
			loop++;
		}
		myfile.close();


Glad you wrote some code, keep it up!
Last edited on
I came up with a little more code, but on my sort Im getting an error that I cant get rid of. Do you see anything wrong with it. Thanks for all your help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <iostream>
#include <string>
#include <cmath>
#include <fstream>
#include <cstdlib>


using namespace std;



class Weapon
{
private:
	string name;
	int hands;
	int attackpower;
	double attackspeed;

public:
	Weapon()
	{
	attackspeed = (rand()%25)*.1;
	}
	double getAts(){return attackspeed;}

	std::string getName()
	{
		return name;
	}
	void setName(std::string setName)
	{
		name = setName;
	}

	int getHands()
	{
		return hands;
	}
	void setHands(int setHands)
	{
		hands = setHands;
	}

	int getAttackpower()
	{
		return attackpower;
	}
	void setAttackpower(int setAttackpower)
	{
		attackpower = setAttackpower;
	}

};


class Hero
{
private:
	int str;
	string lefthand;
	string righthand;

public:
	Hero()
	{
	str = rand()%10+1;
	}
	int getStr(){return str;}

	std::string getLefthand()
	{
		return lefthand;
	}
	void setLefthand(std::string setLefthand)
	{
		lefthand = setLefthand;
	}

	std::string getRighthand()
	{
		return righthand;
	}
	void setRighthand(std::string setRighthand)
	{
		righthand = setRighthand;
	}

};

void sortWeapons(int attackpower[])
{
	bool exchanges;
	do
	{
		exchanges = false;
		for(int i = 0; i < attackpower - 1; i++)
		{
			if(attackpower[i] > attackpower[i + 1])
			{
				double temp = attackpower[i];
				attackpower[i] = attackpower[i+1];
				attackpower[i+1] = temp;
				exchanges = true;
			}
		}
	}
	while(exchanges);
}
	



int main()
{
	
	string array[10];
	short loop = 0;
	string line;

	ifstream myfile ("Weapons.txt");
	if (myfile.is_open())
	{
		while (! myfile.eof() && loop < 11)
		{
			getline (myfile,line);
			array[loop] = line;
			cout << array[loop] << endl;
			loop++;
		}
		myfile.close();
	}
	else cout <<"Unable to open file";
	

	
	return 0;
}
It usually helps to post the error message. It may seem like gobbledygook to you, but someone on here might understand it better, and it will save them a lot of time looking through your code. :)
Line 91: You declare attackpower as an array. Line 97, you attempt to use it as an integer (comparing it to i).

Line 101: Why is temp declared as a double when attackpower is an int? Line 103, you're attempting to assign a double to an int.
Topic archived. No new replies allowed.