I would like to extract a random in a given line so I called a random number to use it as an index. But it keeps giving me an error.
terminated called after throwing an instance of 'std::out_of_range'.
I know it occurs because the index value is exceeded size of the string. But I can not find any mistake in my code.
do {
int size = line.length();
srand(time(NULL));
index = rand() % size;
} while (isspace(line.at(index)));
This is a random number generator. I extract a line from a text file.
I used do { } while (isspace(line.at(index))); because I do not want it to be space, in order to generate a random number again if it is space.
What I want is this.
0123456789
Example... Index : 4
F---i----L F : First, L : Last
if (index == 0) {
while (!isspace(line.at(last + 1))) {
last = last + 1;
}
}
else if (index == (size - 1))) {
while (!isspace(line.at(first - 1))) {
first = first - 1;
}
}
else {
while (!isspace(line.at(first - 1))) {
first = first - 1;
}
while (!isspace(line.at(last + 1))) {
last = last + 1;
}
}
for (int i = first; i <= last; i++) {
targetword += line.at(i);
}
This is how I calculate first and last index and print out the chosen word and it should not make any error...
please help me thank you
Aucun commentaire:
Enregistrer un commentaire