I am bringing a formatted text file and putting different lines to my array in random positions. However, when I try to get rand() % 8
, I get values that I have already had for previous ones. This causes me to lose some lines and leaving some parts of array blank.
How should I modify it so I get unique values from 0 to 7 in random sequence? I am using only what I have learned in class and new stuff is not recommended by instructor. Please help me change the logic of this code.
void get_input(string (&teams)[8][6]) {
srand(time(NULL));
string filename;
double value;
char buffer[100];
int diffnum[8];
string token;
cout << "Enter the input file: ";
cin >> filename;
ifstream infile;
infile.open (filename.c_str());
if (infile.is_open()) {
int teamcounter = 0;
while (infile.getline (buffer, 100) && teamcounter < 8) {
int ran = rand()%8;
for (int find = 0; find < 8; find++) {
while (diffnum[find] == ran) {
ran = rand()%8;
}
}
diffnum[teamcounter] = ran;
int counter = 0;
token = strtok (buffer, ",");
while (counter < 3) {
if (counter == 0) {
teams[ran][counter] = token;
token = strtok (NULL, ", ");
}
else if (counter == 1) {
teams[ran][counter] = token;
token = strtok (NULL, ", ");
}
else {
teams[ran][counter] = token;
}
counter++;
}
teamcounter++;
}
infile.close();
}
else {
cout << "Unable to open file";
exit (EXIT_FAILURE);
}
for (int q =0;q<8;q++) {
cout << diffnum[q];
}
for (int i; i < 8; i++) {
for (int j = 3; j < 6; j++){
teams[i][j] = "0";
}
}
}