| Ver tema anterior :: Ver siguiente tema | | Autor | Mensaje |
|---|
Cyberick
Registrado: 04 Nov 2007 Mensajes: 6 Ubicación: Mayaguez, Puerto Rico
| Publicado: 05/11/2007 9:47 am | | | Título: Validación de Ints |
| Hola a todos, este es mi primer post... los felicito por esta gran comunidad.
Al grano, hace poco comenze con C y mi duda es la siguiente: Cuando yo le pido al usuario que introduzca un número (cualquiera), ¿qué función debo crear para que me valide ese entero, para asegurarme de que el usuario no entre un character y el programa se "vuelva loco"?
Gracias de antemano! -Cyberick
P.S.(1): Digo "vuelva loco" porque no sé el termino en espanol! En ingles sería... "program crash". P.S.(2): Antes de 'postear' busqué en el foro bajo "Validacion de Ints" y "validacion de enteros" pero mi intento fue fallido. Mil disculpas si ya el tema esta en foro... _________________ "Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime." --Chinese Proverb
Ultima edición por Cyberick el 05/11/2007 10:46 am; editado 1 vez |
| | Volver arriba | |  | Cyberick
Registrado: 04 Nov 2007 Mensajes: 6 Ubicación: Mayaguez, Puerto Rico
| Publicado: 05/11/2007 10:45 am | | | Título: |
| Ya lo encontré! Mi duda era similar (sino idéntica) a la del usuario "MasterKiller" en el tema "Validar números de entrada....".
De nuevo... mil disculpas, no volverá a ocurrir. Por cierto, muchas gracias a "rir3760" por aclarar la duda de MasterKiller y por ende, la mía!
Saludos, -Cyberick
<-- Editado -->
Bueno, ya cree mi validador de Ints, con ayuda de algunos posts de este foro por supuesto... Lo dejo por aquí por si alguien lo necesita!
| Código: | /************************************* * * * Algorithm: Intenger's Validator * * Author: Cyberick * * * *************************************/ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> const int STATUS_SUCCESS = 0, STATUS_FAILURE = 1, MAX = 20;
int NumValid( char strNum[ MAX ] );
int main( ) { int num, flag; char sNum[ MAX ];
do {
puts( "Insert a number: " ); gets( sNum );
if( ( NumValid( sNum ) ) == 0 ) { num = atoi( sNum ); printf( "\nThe number entered was: %d\n", num ); flag = STATUS_SUCCESS; }
else { puts( "\nError: Invalid integer\n" ); flag = STATUS_FAILURE; } } while( !( flag == 0 ) );
return EXIT_SUCCESS; }
int NumValid( char strNum[ MAX ] ) { int i; if( ( strlen( strNum ) == 0 ) ) return STATUS_FAILURE;
if( ( strNum[ 0 ] == '-' ) || ( strNum[ 0 ] == '+' ) || ( strNum[ 0 ] == '\0' ) ) return STATUS_FAILURE;
for( i = 0; i < strlen( strNum ); ++i ) if( !isdigit( strNum[ i ] ) ) return STATUS_FAILURE; return STATUS_SUCCESS; } /* Tested with Silverfrost FTN95 */
|
Saludos, -Cyberick _________________ "Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime." --Chinese Proverb |
| | Volver arriba | |  | | |
| No puede crear mensajes No puede responder temas No puede editar sus mensajes No puede borrar sus mensajes No puede votar en encuestas
|
|
| |