I have a problem with a phone book that I am creating. When I ask for the number, if I enter a character, the loop repeats infinitely without giving me the possibility of re-entering the number whatever I do. I have those two blocks of code, the code that allows me to add a contact and the one that checks how many digits the number has to have. I can not understand why it stays with an infinite loop and I have tested with checks whether the std :: cin has failed and so on. Any viable solution?
void AgendaTelefonica::addContacto()
{
Contacto auxiliar;
string nombreAux;
int numAux;
cout<<"Ahora va a agregar un contacto. Introduzca su nombre: ";
cin>>nombreAux;
cout<<"\nIntroduzca su numero de telefono: ";
cin>>numAux;
while((int)contarDigitos(numAux)!=9 || !std::cin)
{
cin.ignore();
cin.clear();
cout<<"Por favor,introduzca un numero de 9 digitos: ";
cin>>numAux;
}
auxiliar.nombre = nombreAux;
auxiliar.numeroTfn = numAux;
int j = 1;
for(int i=0;i<j;i++)
{
if(contactos[i].nombre == "")
{
auxiliar.posicion = i;
}
else{j++;}
}
cout<<"\nLa posicion del contacto es "<<auxiliar.posicion<<endl;
contactos[auxiliar.posicion] = auxiliar;
numContactos++;
}
int AgendaTelefonica::contarDigitos(int numero)
{
int n = numero;
int count = 0;
while (n != 0)
{
n /= 10;
count++;
}
return count;
}