I need to perform the data validation in c of the following program. My question is, how do I validate so that the program only accepts positive integers?
The code of the program is as follows, but I do not know how to make the program not accept negative numbers or letters.
#include<stdio.h>
int main()
{
int i,j,n,time,remain,flag=0,quantum;
int t_espera=0,t_retorno=0,at[10],bt[10],rt[10];
printf("Inserte el numero de procesos:\t ");
scanf("%d",&n);
remain=n;
for(i=0;i<n;i++)
{
printf("Inserte primero el tiempo de llegada y despues inserte el tiempo de rafaga del proceso numero %d :",i+1);
scanf("%d",&at[i]);
scanf("%d",&bt[i]);
rt[i]=bt[i];
}
printf("Inserte el tiempo del Quantum:\t");
scanf("%d",&quantum);
printf("\n\nProceso\t|Tiempo de retorno|Tiempo de espera\n\n");
for(time=0,i=0;remain!=0;)
{
if(rt[i]<=quantum && rt[i]>0)
{
time+=rt[i];
rt[i]=0;
flag=1;
}
else if(rt[i]>0)
{
rt[i]-=quantum;
time+=quantum;
}
if(rt[i]==0 && flag==1)
{
remain--;
printf("P[%d]\t|\t%d\t|\t%d\n",i+1,time-at[i],time-at[i]-bt[i]);
t_espera+=time-at[i]-bt[i];
t_retorno+=time-at[i];
flag=0;
}
if(i==n-1)
i=0;
else if(at[i+1]<=time)
i++;
else
i=0;
}
printf("\nEl tiempo de espera es= %f\n",t_espera*1.0/n);
printf("El tiempo de retorno es = %f",t_retorno*1.0/n);
return 0;
}