I have a txt file that contains the following information in each line:
Example:
John-88888888
I need to read each line of this file and separate the name in one variable and the number in another, for that 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
}
}
In this way the algorithm works perfectly and I get only the name that exists on the line and compare it with another variable and perform an operation there. But I'm having an error 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 error is this
terminate called after throwing an instance of 'std :: out_of_range'
with more information. Variable types are correct. What is causing this? Is there another more practical way?