My problem is that I have to force the user to enter a single integer value, if he puts anything else (characters or decimals) I should have him type again. I already tried to validate scanf
but it did not work. So I did a function to do the validation, but it did not work, so I took the function and left everything in main
same and it is not working either, I have no ideas ...
So the code now, the program accuses any entry 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? And if the number of variables (which is 2: a
b
) is n
where n
is defined by the user, will I be able to use this logic or will I have to use validation by scanf
?