C program in Xcode -
i have problem c code, hope can me. program making basic book "database". when run following code (in xcode), don't know why following sentence gets skipped:
gets(nombre[i]);
on terminal directly prints following if take option 1 menu:
bienvenido al catalogo de libros.
catalogo de tarjetas: 1. introducir 2. buscar por autor 3. buscar por titulo 4. salir
elija opcion:1 warning: program uses gets(), unsafe.
introduzca el nombre del libro:introduzca el autor del libro:
ok, i've tested scanf("%d", &opcion); using printf("%d", opcion); right after proove scanf reads correctly input. surprisingly, reads option introduce correctly. moreover, i've tried running program no "\n" in part see if gets(nombre[i]) works still gets jumped...
any ideas?
this full code (not long):
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> #include <ctype.h> #define max 100 char nombre[max][20]; char autor[max][20]; char edit[max][20]; char buscar[20]; char buscar_t[20]; char buscar_a[20]; int opcion,i,j,k,l; void menu(void); void intro(void); void buscar_autor(void); void buscar_tit(void); void salir(void); void main(void) { printf("bienvenido al catalogo de libros. \n"); menu(); } void menu(void) { printf("\n catalogo de tarjetas:"); printf("\n 1. introducir"); printf("\n 2. buscar por autor"); printf("\n 3. buscar por titulo"); printf("\n 4. salir"); printf("\n elija opcion:"); scanf("%d", &opcion); switch (opcion) { case 1: intro(); break; case 2: buscar_autor(); break; case 3: buscar_tit(); break; case 4: salir(); break; } } void intro(void) { (i=0; i<max; i++) { printf("introduzca el nombre del libro:"); gets(nombre[i]); if (!strcmp(nombre[i],"salir")) { break; } printf("introduzca el autor del libro:"); gets(autor[i]); printf("introduzca la editorial del libro:"); gets(edit[i]); } menu(); } void buscar_tit(void) { printf("introduzca el titulo del libro que quiera buscar:"); gets(buscar_t); (j=0; j<max+1; j++) { if (!strcmp(nombre[j],buscar_t)) { printf("el libro se ha encontrado, el titulo es %s. ", nombre[j]); break; } if (j=max) { printf("el libro no se ha encontrado."); break; } } } void buscar_autor(void) { printf("introduzca el autor del libro que quiera buscar:"); gets(buscar_a); (k=0; k<max+1; k++) { if (!strcmp(autor[k],buscar_a)) { printf("el libro se ha encontrado, el titulo es %s. ", nombre[k]); break; } if (k=max) { printf("el autor no se ha encontrado."); break; } } } void salir(void) { printf("muchisimas gracias por usar el catalogo de libros. \n"); }
hope can me figure out error.
thanks guys.
please avoid using gets. use fgets specifying stdin stream.
char *fgets(char *s, int size, file *stream);
in code modify function:
void intro(void) { getchar(); //added avoid escaping @ stage of loop (i=0; i<max; i++) { printf("introduzca el nombre del libro %d : ",i+1); //modified statement know iteration going on printing loop counter value fgets(nombre[i],sizeof(nombre[i]),stdin); // replaced gets(nombre[i]); if (!strcmp(nombre[i],"salir")) { break; } printf("introduzca el autor del libro: %d : ",i+1); gets(autor[i]); //here use fgets printf("introduzca la editorial del libro %d : ",i+1); gets(edit[i]); //here use fgets } menu(); }
small suggestion:
to make debugging easy, change #define max 100
#define max 5
in case if issue repeats fgets add getchar();
before fgets , see how clear input buffer after fgets overflow?
from man pages of gets
bugs never use gets(). because impossible tell without knowing data in advance how many characters gets() read, , because gets() conĂ¢ tinue store characters past end of buffer, extremely dangerous use. has been used break computer security. use fgets() instead.
Comments
Post a Comment