I have a function that reads a name, if the name is new it saves it in a data structure and if the name is repeated it asks for the name again. (I have other functions similar to this, but this is the only one that uses getline (cin, variable)
void anombre(){
cout << "Nombre: ";
cin.ignore();
getline(cin, nombre);
if(!agenda.vnombre(nombre))
anombre();
}
My problem is that this function only works once; Assuming that I have already stored the name "Mariana" when calling the function, the program would look like this:
Nombre: Mariana
Nombre: Mariana
Telefono:
Being Phone: the message of the following function to request the data, that is, anombre () only denied the name repeated once and went to the next function. Passing this stores an empty string and that goes to my structure.
The agenda.vname (name) function is as follows:
bool vnombre(string& n){
for(int i = 0; i < tamano(); ++i){
if(n == contactos[i].nombre) return false;
}
return true;
}
The other functions are basically the same, only the type of data that you are going to check changes: contacts [i] .phone, contacts [i] .mail, etc.
What is my mistake? I understand that it is part of cleaning the cin buffer for this to work properly. (This does not happen with the other functions to request data such as telephone, email, age, etc.).