I need to validate the numerical data entry in c ++, I am using:
#include<iostream>
#include<stdio.h>
using namespace std;
int main() {
int dato;
cout<<"Ingrese un valor para dato: ";
while( ( cin>>dato ).fail() ) { //comprobamos los flags de error con la referencia que devuelve el operador >>
cin.clear(); //reseteamos los flags
fflush(stdin); //limpio buffer de entrada
cout<<".:Entrada Invalida:."<<endl;
cout<<"Ingrese un valor para dato: ";
}
cout<<"\n\tEl Dato es: "<<dato;
return 0;
}
This works correctly for one-character entries and strings, also for Alpha-Numeric strings but only when numbers are entered after a character.
Example:
Ingrese un valor para dato: t
.:Entrada Invalidada:.
Ingrese un valor para dato: ttt
.:Entrada Invalidada:.
Ingrese un valor para dato: rgwsw934954
.:Entrada Invalidada:.
If I put an entry as 3x where I first enter a number and then the characters, it takes answer 3, but I need to ask for a new data.
Ingrese un valor para dato: 1234qwerty
El Dato es: 1234