reading in a binary file, creating a vector

hello, i think my first issue is creating a vector in a class...with what i have now something is going wrong with the access i think, i need shipWeapons to be a part of ship so each ship has a shipWeapons vector but i am doing something wrong

these are the instructions:
1. Print all the ships
2. Print the starship with the most powerful weapon
3. Print the most powerful ship (highest combined power rating of all weapons)
4. Print the weakest ship (out of ships that actually have weapons)
5. Print the unarmed ships
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256

#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

vector <ship> ships;
//vector <weapons> shipWeapons;

void printAll();
void strongestWeapon();
void bestAllAround();
void weakestShip();
void unArmed();


class weapons {
public:
	string weaponName;
	int firePower;
	float consumeFloat;
};

class ship {
public:
	//void read(ifstream &file)

	string name;
	string classType;
	short length;
	int shield;
	float speed;
	vector <weapons> shipWeapons;                                // how to make ship weapons part of ships
	///int *weaponsPtr = shipWeapons[0];
};

void read(ifstream &file) {
	ship singleShip;
	weapons singleWeapon;

	int nameLength;
	int classLength;
	int tempClass;
	int tempLength;
	int tempShield;
	int tempSpeed;
	int weaponsNum;

	while (!file.eofbit) {
		file.read((char *)nameLength, 4);
		char *tempName = new char[nameLength]; //to allocate without tknowing the size
		file.read(tempName, nameLength);
		file.read((char *)classLength, 4);
		char *tempClass = (char *)malloc(classLength);
		file.read(tempClass, classLength);
		file.read((char *)tempLength, 2);
		file.read((char *)tempShield, 4);
		file.read((char *)tempSpeed, 4);
		file.read((char *)weaponsNum, 4);

		singleShip.name = tempName;
		delete tempName;
		singleShip.classType = tempClass;
		delete tempClass;
		singleShip.length = tempLength;
		singleShip.shield = tempShield;  //but if change it here it will lose information
		singleShip.speed = tempSpeed;

		for (int i = 0; i < weaponsNum; i++) {
			
			int weaponNameLength;
			int tempWeaponPower;
			int tempWeaponConsume;

			file.read((char *)weaponNameLength, 4);
			char *tempWeaponName = new char[weaponNameLength];
			file.read(tempWeaponName, weaponNameLength);
			file.read((char*)tempWeaponPower, 4);
			file.read((char *)tempWeaponConsume, 4);

			singleWeapon.weaponName = tempName;
			singleWeapon.firePower = tempWeaponPower;
			singleWeapon.consumeFloat = tempWeaponConsume;

			shipWeapons.push_back(singleWeapon);           //has to be a part of ships though 
		}

		ships.push_back(singleShip);
	}
};

void printAll() {
	for (int i = 0; i < size(ships); i++) {
		cout << "Name: " << ships[i].name << endl;
		cout << "Class: " << ships[i].classType << endl;
		cout << "Length: " << ships[i].length << endl;
		cout << "Shield Capacity: " << ships[i].shield << endl;
		cout << "Maximum Warp: " << ships[i].speed << endl;
		cout << "Armaments: " << endl;

		if (shipWeapons.empty()) {
			cout << "Unarmed" << endl << endl;
		}
		else {
			int totalFirePower = 0;

			for (int i = 0; i < shipWeapons.size(); i++) {
				cout << shipWeapons[i].weaponName << ", ";
				cout << shipWeapons[i].firePower << ", ";
				cout << shipWeapons[i].consumeFloat << endl;

				totalFirePower += shipWeapons[i].firePower;
			}

			cout << "Total firepower: " << totalFirePower << endl << endl;
		}
	}
}

void strongestWeapon() {
	//store the strongest weapon to compare to next weapons
	weapons strongestWeapon = ships[0].shipWeapons[0];

	for (int i = 0; i < ships.size(); i++) {
		for (int j = 0; j < shipWeapons.size(); i++) {
			if (ships[i].shipWeapons[j].firepower > strongestWeapon.firePower) {
				strongestWeapon = ships[i].shipWeapons[j].firepower;
			}
		}
	}
	
	//wait that is just the weapon....
	cout << strongestWeapon << endl; //no good??
}


//I DONT KNOW WHAT IM DOING
void bestAllAround() {
	//highest combined power rating of all weapons
	int strongestArsenal = 0;
	int myArsenal = 0;
	ship bestAA;

	for (int i = 0; i < ships.size(); i++) {
		for (int j = 0; j < shipWeapons.size(); i++) {

			myArsenal += ships[i].shipWeapons[j].firepower;

			if (myArsenal > strongestArsenal) {
				bestAA = ships[i];
			}
		}
	}
}


void weakestShip() {
	//weakest not including ones without weapons

	vector <ship> armed;
	weapons weakest = ships[0].shipweapons[0];

	for (int i = 0; i < ships.size(); i++) {
		for (int j = 0; j < shipWeapons.size(); i++) {
			if (ships[i].shipWeapons != NULL) {
				armed.push_back(ships[i]);
			}
				
			if (armed[i].shipWeapons[j].firepower < weakest.firePower) {
				weakest = armed[i].shipWeapons[j];
			}
		}
	}
}


void unArmed() {
	//if unarmed
	vector <ship> unarmed;

	for (int i = 0; i < ships.size(); i++) {
		for (int j = 0; j < shipWeapons.size(); i++) {
			if (ships[i].shipWeapons == NULL) {
				unarmed.push_back(ships[i]);
			}

		}
	}

}

int main()
{

	cout << "Which file(s) to open?\n";
	cout << "1. friendlyships.shp" << endl;
	cout << "2. enemyships.shp" << endl;
	cout << "3. Both files" << endl;
	int option;
	cin >> option;

	/* Load files here */

	ifstream fileEnemy;
	ifstream fileFriendly; 

	fileEnemy.open("enemyships.shp");
	fileFriendly.open("friendlyships.shp");

	if (option == 1) {
		read(fileEnemy); // , vector <ship> enemyShips);
	}

	if (option == 2) {
		read(fileFriendly); //, vector <ship> friendlyShips);
	}

	if (option == 3) {
		read(fileFriendly);
		read(fileEnemy);
	}

	cout << "1. Print all ships" << endl;
	cout << "2. Starship with the strongest weapon" << endl;
	cout << "3. Strongest starship overall" << endl;
	cout << "4. Weakest ship (ignoring unarmed)" << endl;
	cout << "5. Unarmed ships" << endl;

	cin >> option;

	/* Work your magic here */

	switch (option)
	{
	case 1:
		printAll();
		break;
	case 2:
		strongestWeapon();
		break;
	case 3:
		bestAllAround();
		break;
	case 4:
		weakestShip();
		break;
	case 5:
		unArmed();
		break;
	default:
		break;
	}

	return 0;
}
Last edited on
also how do i format this
If you mean how do you format your code you put it between "code tags" like this:

[code]
put your code here
[/code]

(Or you can use the <> format button, which automatically adds code tags around whatever is highlighted.)
Last edited on
Topic archived. No new replies allowed.