2012-12-20 43 views
1

我有這個功能,它是一個菜單。編譯後,以下錯誤不斷顯示:錯誤:指針和整數之間的比較[缺省情況下啓用]。爲什麼發生這種情況?菜單功能錯誤:指針和整數之間的比較[默認啓用]

char choice; 

    printf ("Welcome to the Customer menu! \n"); 
    printf ("Please select option from below\n"); 
    printf ("a. Add customer\n"); 
    printf ("b. Modify customer\n"); 
    printf ("c. List customers\n"); 
    printf ("d. Go back to main menu"); 

    while ((gets(&choice)) != 'q') 
      { 
       if (choice == '\n') 
        continue; 
       switch (choice) 
       { 

     case 'a' : add_customer(); 
        break; 
     case 'b' : printf ("products_main()"); 
        break; 
     case 'c' : printf ("orders_main()"); 
        break; 
     default : printf ("Invalid input. Please enter an option from the above menu\n"); 
        continue; 

       } 

       printf ("END PROGRAM"); 

謝謝!

+0

順便說一句,你爲什麼不閱讀你嘗試使用的函數的文檔? – 2012-12-20 20:49:56

回答

0

這條線:

while ((gets(&choice)) != 'q') 

得到()讀取字符串,而不是一個char,並返回該字符串(即它填充你通過字符指針傳遞給它的緩衝器)。然後,您將返回的指針(與傳入的指針相同)與char進行比較。

你可能只想讀一個字符。如果你想要一個完整的字符串,你需要將它讀入一個char數組,而不是傳遞一個字符的地址。

1

gets()函數返回一個char *,而你是比較返回值和一個char

if (gets(&choice)) != 'q') 

另外請注意,這是錯誤的兩個級別,因爲gets()stdin讀取,直到遇到一個換行符,所以如果你傳遞一個char的地址,它可能會導致緩衝區溢出錯誤。爲什麼不用fgets()代替?

char buf[128]; 
fgets(buf, sizeof(buf), stdin); 
if (buf[0] == 'q') { 
    /* etc */ 
} 
1

不能使用gets()來做到這一點,畢竟和得到()是很危險的,不檢查多少字符閱讀,可能會導致一個非常惡劣的運行時緩衝區溢出。

您應該使用像H2CO3這樣的fgets(),它具有讀取字符的限制,因此更安全。

char * input(const char *message, size_t quantity) 
{ 
    const int BUFFER_SIZE = 512; 
    char buf[BUFFER_SIZE], *res = NULL; 

    if(quantity > BUFFER_SIZE || quantity == 0) 
     quantity = BUFFER_SIZE - 1; 

    if(message) 
     printf("%s",message); 

    if(fgets(buf, quantity + 1, stdin) > 0) 
    { 
     char *end = strchr(buf, '\n'); 
     if(end){ 
      *end = '\0'; 
     } 

     res = malloc(strlen(buf) + 1); 
     if(!res) 
     { 
      fprintf(stderr, "input(): MEM alloc error\n"); 
      return NULL; 
     } 
     strcpy(res, buf); 

    } 
    return res; 
} 

試試這個功能,只是傳遞你想要的信息,以及你想要的輸入字符的確切數量。 :)

如果你想嘗試一下孤獨,在這裏你有一個測試程序:

#include<stdio.h> 
#include<string.h> 
#include<stdlib.h> 

char * input(const char *message, size_t quantity) 
{ 
    const int BUFFER_SIZE = 512; 
    char buf[BUFFER_SIZE], *res = NULL; 

    if(quantity > BUFFER_SIZE || quantity == 0) 
     quantity = BUFFER_SIZE - 1; 

    if(message) 
     printf("%s",message); 

    if(fgets(buf, quantity + 1, stdin) > 0) 
    { 
     char *end = strchr(buf, '\n'); 
     if(end){ 
      *end = '\0'; 
     } 

     res = malloc(strlen(buf) + 1); 
     if(!res) 
     { 
      fprintf(stderr, "input(): MEM alloc error\n"); 
      return NULL; 
     } 
     strcpy(res, buf); 
    } 
    return res; 
} 

int main() 
{ 
    char *a = input("Input:", 4); 
    if(a) 
    { 
     printf("%s\n",a); 
     free(a); 
     return 0; 
    } 
    printf("Got NULL input\n"); 
    return -1; 
} 

當你有一個關於特定功能的疑問,他們有什麼參數,它們的返回值,你可以看看它在谷歌,你會發現很多的例子和函數定義。隨着時間的推移,您將學會輕鬆理解定義並記住一些函數名稱及其參數。

祝你好運!

0

做一些閱讀後,我發現,包括

#include <unistd.h> 

有助於得到警告的車程。我是unix c的新手,我從未見過它。我還在測試我的代碼,所以當我弄清楚它是否有效時,我會回覆你。

希望這會有所幫助。

最後警告回來了,它最終陷入了一個無限循環,所以我的邏輯出了問題。

對不起,我沒有任何幫助。

+0

您是否回答了這個錯誤的問題? – Blastfurnace

相關問題