-1
I have a file txt
containing the following type of information on each line:
Example:
John-88888888
I need to read every line of that file and separate the name in a variable and the number in another, I used the substring()
, to get the name I did the following.
while(!temp.eof()){
getline(temp, linha);
posl = linha.find('-');
if((linha.substr(0, posl).compare(nome))){
//faço algo aqui
}
}
So the algorithm works perfectly and I take only the name that exists on the line and compare it with another variable and perform an operation there. But I’m making a mistake when I try to get the number. Code:
while(!temp.eof()){
getline(temp, linha);
posl = linha.find('-');
if(linha.substr(posl+1).compare(numero)){ //aqui quebra
//faço algo
}
}
The mistake is this
terminate called after Throwing an instance of 'Std::out_of_range'
With more information. The variable types are correct. What is causing this? Is there another way more practical?