2015-12-17 20 views
0

我想使用嵌套的switch語句爲用戶編寫一個菜單做選擇一些選項。第一個開關工作正常,並且嵌套的一個沒有。 在嵌套開關中,我總是以default選項結束,用戶不能選擇。爲什麼第二個getchar()調用總是觸發我的交換機的默認情況?

看起來變量d保持爲NULL,這就是使交換機在默認選項中結束的原因。什麼是防止用戶能夠鍵入一個字符,並從代碼分配值d

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

/* function declaration */ 
int connection_func(); 
int main() { 
    /*make connection to sql server*/ 
    int connection_return = connection_func(); 
    printf("%d",connection_return); 

     char c,d; 
     printf("\n Choose one of the following options: \n"); 
    printf("1- DB maintenance \n"); 
    printf("2- Weekly schedule creation \n"); 
    c= getchar(); 
    switch(c) { 
      case '1': 
        // DB maintenance 
        printf("\n DB maintenance options:: \n"); 
        printf("1- ADD data to existing table \n"); 
        printf("2- DELETE data from existing table \n"); 
        printf("3- DISPLAY all data inside a table \n"); 
        printf("4- DROP table (root only) \n"); 
        d = getchar(); 
        switch(d) { 
          case 'A': 
          // User want to ADD data to the database 
          break; 
          case 'B': 
          // User want to DELETE data from database 
          break; 
          case 'C': 
          // User want to Display the tbl data 
          break; 
          case 'D': 
          // User want to DROP tables 
          break; 
          default: 
          printf("That is not a proper selection \n"); 
          break; 
    } 
        break; 

      case '2': 
        // Weekly schedule creation 
        break; 
      default: 
        printf("That is not a proper selection. \n"); 
        break; 
    } 

return(0); 
} 

int connection_func() { 
    MYSQL   *conn; 
    MYSQL_RES  *res; 
    MYSQL_ROW  row; 
    char *server = "localhost"; 
    char *user  = "root"; 
    char *password = "mypassword"; 
    char *database = "sc" ; 

    conn = mysql_init(NULL); 
    /* Connect to database */ 
    if (!mysql_real_connect(conn, server, user, password, database, 0, NULL , 0)) { 
      fprintf(stderr, "%s\n", mysql_error(conn)); 
      return(1); 
    } 
return(0); 
} 

編譯沒有錯誤。從終端的輸出:

[email protected]:# ./sc 
0 
Choose one of the following options: 
1- DB maintenance 
2- Weekly schedule creation 
1 
(null) -d  (**** - the value of d variable ***) 
DB maintenance options:: 
1- ADD data to existing table 
2- DELETE data from existing table 
3- DISPLAY all data inside a table 
4- DROP table (root only) 
That is not a proper selection 
+1

你是如何確定'D',一個'char',具有價值'NULL'? –

+0

我打印它並得到:(null) – ic205

+1

然後你打印不正確。你用過'%s'嗎?這是爲了字符串,而不是字符。要打印字符值:'printf(「d ='%c'\ n」,c);'將其打印爲整數:'printf(「d =%d \ n」,d);'。 –

回答

0

插入'1'並單擊輸入後,標準輸入流在其中包含「1 \ n」, 然後調用一次getchar(),從標準輸入刪除「1」。

然後,下一次調用getchar()不會提示用戶,因爲stdin流中已經有數據 - 換行符「\ n」。

嘗試使用

while((d = getchar()) != '\n' && d != EOF); 

紡出的無用數據,直到換行符,通常它會只是「\ n」字符,所以你甚至可以用執行只是多了一個調用的getchar( )從流中刪除換行符。

此外,在其他評論中提到,我會考慮fgets

1

注意,你沒有機會甚至可以說AB。按下1後按Enter1得到cd = getchar()結果d有一個換行符的代碼。

+0

我怎樣纔能有機會選擇?在第一個開關的方式工作正常,在第二個不是? – ic205

+0

讀取並放棄該換行符:'c = getchar(); getchar()',或者更好的使用'fgets()/ scanf()'。 – user58697

0

當你輸入「1」時,你實際上給你的程序提供了兩個字符,一個「1」後面跟着一個輸入(它在標準輸入顯示爲一個換行符)。 換行然後通過第二次調用返回到d = getchar()。 (您可以輸入這兩個選項來查看您的程序的工作情況:例如12,然後按ENTER鍵)。

如果你想跳過空格,如換行符,製表符和空格,我建議使用scanf(" %c", &d)。最初的空間告訴scanf在轉換單個字符之前跳過任意數量的空格。請務必檢查scanf是否返回1,表示已成功將項目轉換爲掃描。

相關問題