I have a problem that I need to validate an entry to receive only integers, if I do not receive it, I should force the user to type an integer. I can not disregard numbers after the comma (in the case of decimals) and I can not return the entire equivalent of a letter (in case the user reports letter).
int lerInteiro();
int main()
{
int numero=0;
numero=lerInteiro();
printf("Numero: %i .\n",numero);
return 0;
}
int lerInteiro()
{
int numero=0;
int flag=0;
do
{
fflush(stdin);
puts("Digite um numero inteiro: \n");
if(scanf("%i",&numero)!=EOF)
{
flag=1;
puts("Numero invalido!\n");
puts("Digite apenas NUMEROS\n!");
puts("Digite apenas numeros INTEIROS!\n");
}
else
flag=0;
}while(flag==1);
return numero;
}
The program should stop when an integer is entered and return the number entered.