I would like to know the best and most efficient way to read a type of data and if you type anything else that does not pass from there, until you type the type of data that I am asking for.
Example:
int numero;
printf("Digite un numero: ");
scanf("%d",&numero);
If the user types a letter, that does not accept it; if the user types in signs such as:., '{[]}] # € ~ €€ ~~ @ # @ | among others, that does not accept it; the same goes for the data type char
, which I could not control very well because it accepts numbers and signs.
What I'm looking for is that the data reading is fairly compact and secure.
I am implementing the following statement to read a specific data type but I need you not to accept the signs: .'{{{{{{{{# | € @ # € '%%% of ++', etc .
do //Utilizo un 'do-while' para validar el ingreso del tipo de dato.
{
system("CLS");
printf("DIGITE EL NUMERO DE CONJUNTOS QUE DESEA CREAR (DEBE SER MAYOR A '0' - HASTA 20 ): ");
}
while(!scanf("%d",&vectores)==1 || vectores==0 || vectores>20);
As long as scanf does not return 0, it will continue there; I do not know if it's the best way but it works very well since it's the shortest sentence I've seen to validate a type of data.