Tema: Duda!!!
Ver Mensaje Individual
  #2 (permalink)  
Antiguo 17/11/2010, 17:01
Trovaz
 
Fecha de Ingreso: octubre-2010
Ubicación: Edo. de México
Mensajes: 94
Antigüedad: 13 años, 7 meses
Puntos: 9
Respuesta: Duda!!!

Es muy facil de realizar mediante scanf.

Hay que recordar que scanf esta definido de la siguiente manera:
Código:
int scanf(const char *format, ...);
Esto nos muestra que regresa un entero, pero cual?, Para saber que regresa exactamente consultamos la pagina del man de scanf.

Código:
RETURN VALUE
       These  functions  return the number of input items successfully matched
       and assigned, which can be fewer than provided for, or even zero in the
       event of an early matching failure.

       The  value EOF is returned if the end of input is reached before either
       the first successful conversion or a matching failure occurs.   EOF  is
       also returned if a read error occurs, in which case the error indicator
       for the stream (see ferror(3)) is set, and errno is  set  indicate  the
       error.
Entonces solo tienes que verificar el valor de retorno, si pides un entero sabras si te enviaron en realidad un entero o te ingresaron algun caracter o cadena (si te ingresan un numero decimal solo se toma la parte entera y se la asigna a tu variable). Ejemplo.

Código c:
Ver original
  1. int salida = 0;
  2. int miEntero = 0;
  3. while(salida != 1){
  4.     printf("Digite un entero: ");
  5.     salida = scanf("%i", &miEntero);
  6.     fflush(stdin);
  7.     if (salida != 1){
  8.        printf("No se ha digitado un entero, intente otra vez\n");
  9.     }
  10. }


Espero te sea de ayuda

Saludos++