-1

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++;
    }
}
Nopileos
  • 1,976
  • 7
  • 17
  • 4
    `int k;` leaves `k` uninitialized, which is a problem when `random[k]` happens on the next line. Also `int i, j = 0;` only initializes `j` but not `i`, which could lead to more problems. These may or may not be the only problems you're having, but they need to be fixed regardless. – TheUndeadFish Feb 06 '20 at 05:00
  • First step in debugging a crash is figuring out which line the crash occurred on. You can use your debugger for that, or you can start putting in temporary debug-prints (e.g. `cout << __LINE__ << endl;` at various places in the code, and seeing which one gets printed last before the crash, until you've narrowed down the line where the crash happens. Once you know that, you will probably be able to figure out *why* that line is crashing. – Jeremy Friesner Feb 06 '20 at 05:07
  • Please note that `srand(time(NULL));` should be moved to the beginning of the `main` function. There's no need to execute it more than once. See e.g. https://stackoverflow.com/questions/13445688/how-to-generate-a-random-number-in-c – Bob__ Feb 06 '20 at 15:44

1 Answers1

0

As already pointed out in the comments. i and k are not initialized. So there is no guarantee they will be 0, which you expect in the code. They could be anything a int can be.

If you want you can just add a

std::cout << k << " " << i << std::endl;

after int k and have a look for yourself.

If I am running it my i and k are something around 32000. In the line

random[k] = rand() % 50

it crashes for me. In the future if you have a similar problem you might want to use a debugger and just start your program and if it is crashing you will exactly see where it crashed and see the values of the all variables available inside the current scope.

Nopileos
  • 1,976
  • 7
  • 17