I have code where I search through a file assign each line to an array then I randomly output groups based on how many people you want per group. EX: If the user enters 2 people per group and there are 50 people in the list then there will be 25 groups of 2s.
There is a segmentation fault somewhere along the lines but I don't know where or honestly what it is.
using namespace std;
int peoplePerGroup;
int count = 0;
int groupNum;
void group();
string names[50];
int main() {
cout << "How many people do you want per group: ";
cin >> peoplePerGroup;
string line;
ifstream myfile("names.txt", ios::in);
if (myfile.is_open()) {
int i = 0;
while (getline(myfile, line)) {
if (i < 50) {
names[i] = line;
i++;
} else
continue;
}
myfile.close();
}
group();
return 0;
}
void group() {
int i, j = 0;
while (j < 50) {
srand(time(NULL));
int random[50];
int k;
random[k] = rand() % 50;
groupNum = 1;
if (peoplePerGroup < i) {
groupNum++;
i = 0;
cout << "Group " << groupNum << " has: ";
cout << names[random[k]] << " ";
names[random[k]] = "done";
j++, i++;
}
if (peoplePerGroup >= i) {
if (names[random[k]] != "done") {
cout << " " << names[random[k]] << " ";
names[random[k]] = "done";
j++, i++;
}
}
k++;
}
}