I'm doing an c++ assignment and I'm a complete newbie to c++. The idea of the assignment is to create an 11 players team form 22 available players. You've to choose 1 from 3 available goalkeepers, 4 from 7 defense players, 4 from 8 middle field players and 2 from 4 attackers (this is a soccer team). Now the possible combinations for all of that are 44100. It's a requirement that I used enumerations for the mentioned team parts and I've to give a unique output each time. I'd tried many approaches, but ended up using vectors to do the job. The logic seems to be write to me and the debugging doesn't really help as I get segmentation fault. It would be great if someone could give a hand. I hope the description is clear enough. Here is the code:
#include <string>
#include <vector>
#include <ctime>
#include <iostream>
enum goal {
Neuer, terStegen, Trapp
};
enum def {
Boateng, Ginter, Hector, Hummels, Ruediger, Schulz, Suele
};
enum mid {
Brandt, Can, Draxler, Goretzka, Guendogan, Kroos, Mueller, Rudy
};
enum att {
Gnabry, Reus, Sane, Werner
};
std::string chooseGoal(int x) {
switch (x) {
case 0: return "Neuer";
case 1: return "ter Stegen";
case 2: return "Trapp";
}
}
std::string chooseDef(int x) {
switch (x) {
case 0: return "Boateng";
case 1: return "Ginter";
case 2: return "Hector";
case 3: return "Hummels";
case 4: return "Ruediger";
case 5: return "Schulz";
case 6: return "Suele";
}
}
std::string chooseMid(int x) {
switch (x) {
case 0:return"Brandt";
case 1:return "Can";
case 2:return "Draxler";
case 3:return "Goretzka";
case 4:return "Guendogan";
case 5:return "Kroos";
case 6:return "Mueller";
case 7:return "Rudy";
}
}
std::string chooseAtt(int x) {
switch (x) {
case 0: return "Gnabry";
case 1: return"Reus";
case 2: return "Sane";
case 3: return "Werner";
}
}
std::vector<std::string> createVectorDef() {
std::vector<std::string> v;
std::srand(std::time(NULL));
for (int i = 0; i < 4; i++) {
int x = rand()%7;
v.push_back(chooseDef(x));
}
return v;
}
std::vector<std::string> createVectorMid() {
std::vector<std::string> v;
std::srand(std::time(NULL));
for (int i = 0; i < 4; i++) {
int x = rand() % 8;
v.push_back(chooseMid(x));
}
return v;
}
std::vector<std::string> createVectorAtt() {
std::vector<std::string> v;
std::srand(std::time(NULL));
for (int i = 0; i < 2; i++) {
int x = rand() % 4;
v.push_back(chooseAtt(x));
}
return v;
}
std::vector<std::string> makeUniqueDef(std::vector<std::string> v) {
for (int i = 0; i < v.size()-1; i++) {
for (int j = i + 1; j < v.size(); j++) {
if (v[i] == v[j]) {
// v.clear();
v = createVectorDef();
makeUniqueDef(v);
} else {
continue;
}
}
}
return v;
}
std::vector<std::string> makeUniqueMid(std::vector<std::string> v) {
for (int i = 0; i < v.size()-1; i++) {
for (int j = i + 1; j < v.size(); j++) {
if (v[i] == v[j]) {
v = createVectorMid();
makeUniqueMid(v);
} else {
continue;
}
}
}
return v;
}
std::vector<std::string> makeUniqueAtt(std::vector<std::string> v) {
for (int i = 0; i < v.size()-1; i++) {
for (int j = i + 1; j < v.size(); j++) {
if (v[i] == v[j]) {
v = createVectorAtt();
makeUniqueAtt(v);
} else {
continue;
}
}
}
return v;
}
void displayVector(std::vector<std::string> v) {
for (int i = 0; i < v.size(); i++) {
if (i == v.size() - 1) {
std::cout << v[i] << '.' << std::endl;
break;
}
std::cout << ' ' << v[i] << ',';
}
}
int main() {
// Random Gen
std::srand(std::time(NULL));
std::cout << "Tor: " << chooseGoal(rand() % 3) << std::endl;
//Vector for defence and display
std::vector<std::string> def = createVectorDef();
def = makeUniqueDef(def);
std::cout << "Abwehr:";
displayVector(def);
// Vector for mid and display
std::vector<std::string> mid = createVectorMid();
mid = makeUniqueMid(mid);
displayVector(mid);
std::cout << "Mittelfeld:";
displayVector(mid);
// Vector for atta and display;
std::vector<std::string> att = createVectorAtt();
att = makeUniqueAtt(att);
// std::cout << "Angriff:";
displayVector(att);
}