# Validate positive integers

1

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;
}
``````

asked by zydeico 18.02.2017 в 14:59
source

2

For any integer:

``````int foo;
``````

You can do:

``````if(foo > 0 && !isalpha(foo))
{
//Haces lo que quieras
//Aqui ya habra filtrado si el caracter es mayor a cero y NO es alfabetico
}
``````

The int isalpha (int) function is defined in ctype.h

``````#include <ctype.h>
``````

Another thing that I would change would be that scanf

``````int caracter = getchar();
``````

This line reads a character from the keyboard. This feature is included in stdio.h

``````#include<stdio.h>
``````

References:

1

Taken from the `scanf` documentation:

On success, the function returns the number of items of the argument list successfully filled. This count can match the expected number of items or less (even zero) due to a matching failure, a reading error, or the reach of the end-of-file.

That is, if the function returns a value equal to or less than 0, it is likely that the reading has failed.

This feature can be used to know if what the user tries to enter is a number or not:

``````int valor;
int res = scanf("%d",&valor);

if( res <= 0 )
puts("El dato no es numerico\n");
else
printf("Has introducido el numero %d\n",valor);
``````

Do not forget that, in this case, you should clean the input buffer to avoid dirty readings. As `fflush` is not appropriate to use it in entry buffers you could do something such that:

``````char c;
while ((c = getchar()) != '\n' && c != EOF) { }
``````

Well, once you have verified that the data is numeric, you have to make sure it is positive:

``````if( valor < 0 )
puts("Por favor, introduce un numero positivo\n");
``````

Another possibility is to read a string of characters and interpret the characters on our own:

``````char buffer[20];
scanf("%s",buffer);

int valor = 0;

for(char* ptr = buffer; *ptr; ++ptr)
{
if( !isalpha(*ptr) )
{
puts("El dato no es numerico\n");
valor = -1;
break;
}
else
{
valor *= 10;
valor += *ptr - '0';
}
}

if( valor >= 0 )
// No ha habido errores
``````

As soon as the digit is not numeric (either because you enter a character or the minus sign `-` the program will show the error message.