1
My problem is that I have to force the user to enter a whole value only, if he puts anything else (characters or decimals) I have to make him type again. I’ve already tried to validate scanf
but it didn’t work. So I did a function to do the validation, but it didn’t work, main
same and also this not working out, I have no more ideas...
So this code now, the program accuses any input as invalid, even if it is an integer.
int maiorNumero(int,int);
int main()
{
int a=0,b=0;
int flag=0,maior=0;
do
{
fflush(stdin);
puts("Digite um numero inteiro:\n");
scanf("%i",&a);
puts("Digite um numero inteiro:\n");
scanf("%i",&b);
float f=a;
float f2=b;
char c=a;
char c2=b;
if(f!=a || f2!=b || c!=a || c2!=b)
{
flag=1;
puts("Numero invalido!\nDigite apenas NUMEROS INTEIROS.\n");
}
else
flag=0;
}while(flag==1);
maior=maiorNumero(a,b);
printf("O maior numero digitado foi: %i .",maior);
return 0;
}
int maiorNumero(int a, int b)
{
int maior;
if(a>b)
{
maior=a;
}
else
if(a<b)
{
maior=b;
}
return maior;
}
What am I missing? What if the number of variables (which is 2:a
b
) for n
where n
is user-defined, I will be able to use this logic or I will have to use validation by scanf
?