2013-08-28 18 views
1

我的C代碼有問題,希望你能幫助我。該計劃是關於製作一本基本的書「數據庫」。 當我運行下面的代碼(在Xcode中),我不知道爲什麼下面的句子被跳過:Xcode中的C程序

gets(nombre [i]);

在終端直接打印如果我帶選項1從菜單中選擇下列:

比恩韋尼人catalogo德libros。

Catalogo德tarjetas: 1. Introducir 2. Buscar POR作者日期 3. Buscar POR TITULO 4. Salir

Elija opcion:1個 警告:本程序使用得到(),這是不安全的。

Introduzca EL農佈雷德爾LIBRO:Introduzca EL作者日期德爾LIBRO:

好了,我已經測試了我的scanf( 「%d」,& opcion);使用printf(「%d」,opcion);之後立即提出該scanf正確讀取我的輸入。令人驚訝的是,它讀取我正確引入的選項。此外,我試過運行該程序沒有「\ n」的任何部分,看看是否得到(nombre [i])的作品,但它仍然會跳躍...

任何想法?

這是一個完整的代碼(不長):

#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) 
{ 
     for (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); 

    for (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); 

    for (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"); 
} 

希望你能幫助我找出錯誤。

謝謝你們。

+3

你好,歡迎來到stackoverflow.com。請花些時間閱讀[幫助頁面](http://stackoverflow.com/help),尤其是名爲[「我可以問些什麼話題?」]的章節(http://stackoverflow.com/help/)討論話題)和[「我應該避免問什麼類型的問題?」](http://stackoverflow.com/help/dont-ask)。另請閱讀[Stack Overflow問題清單](http://meta.stackexchange.com/questions/156810/stack-overflow-question-checklist)。您可能還想了解[SSCCE](http://sscce.org/)是什麼。 –

+1

有關清單和SSCCE的要點很重要,因爲您應該只嘗試發佈描述問題的*最小*代碼示例。沒有多少人願意閱讀許多頁面的代碼。另外,這裏沒有多少人能夠閱讀你錯誤信息的語言(西班牙語?葡萄牙語?),但是每個人都懂英語。編譯之前,您可能需要更改系統上的語言環境設置以獲取英文錯誤消息。 –

+0

在這種情況下,你是否試圖用['fgets'](http://en.cppreference.com/w/c/io/fgets)替換'gets'? –

回答

1

請避免使用gets。使用fgets指定stdin作爲流。

char *fgets(char *s, int size, FILE *stream); 

在你的代碼修改該函數:

void intro(void) 
{ 
     getchar(); //added this to avoid escaping at early stage of loop 
     for (i=0; i<MAX; i++) 
     { 
      printf("Introduzca el nombre del libro %d : ",i+1); //modified this statement to know which iteration is going on by printing the 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 also use fgets 
      printf("Introduzca la editorial del libro %d : ",i+1); 
      gets(edit[i]); //here also use fgets 
     } 

    menu(); 

} 

小建議:

,使調試容易,改變#define MAX 100#define MAX 5

在看到這個Safe Alternative to gets

您情況如果用fgets重複該問題只需添加getchar();
與fgets之前也看到這也How to clear input buffer after fgets overflow?

從該名男子頁

得到

BUGS 不要使用gets()函數。因爲事先不知道數據是不可能知道get()將讀取多少個字符的,並且由於gets()會將緩衝區末尾的字符存儲起來,所以使用它非常危險。它已被用來破壞計算機安全。使用fgets()代替。

+1

很抱歉,我在討論C時曾是begginer。我試圖改變fgets()的gets(),但是我發現fgets()需要比get()更多的參數。無論如何,我一直使用gets(),因此程序應該工作,錯誤必須在gets(nombre [i])之前;句子。謝謝! –

+0

@JorgeSebastián編輯我的答案,檢查修改後的代碼。 – Gangadhar