2017-02-24 79 views
0

我被賦予了一項家庭作業任務,其任務是創建一個簡單的hang子手遊戲。以下是我的代碼。我目前正在嘗試使用getchar()來請求用戶輸入一個小寫字母,並查看它是否與他們應該猜測的單詞中的某個字母匹配。從我非常有限的經驗來看,我擁有的代碼應該可以工作,並且當我逐步完成程序時,它看起來應該正常運行,但是當我真正運行該程序時,似乎跳過第二個getchar()。任何人有任何建議或幫助?C getchar()hang子手遊戲

#include "stdio.h" 
#include "math.h" 

int main(void) { 
    int(a); 
    int(b); 
    float(x); 
    float(c); 
    float(e); 

    int word[4] = {116, 101, 115, 116}; 
    int guess[4]; 

    c == 0; 
    a == 0; 
    b == 0; 

    printf("Welcome to Hangman\n"); 
    printf("Input a word--one lower case letter at a time\n"); 
    printf("Enter Guess: "); 

    x = getchar(); 

    if (x > 122) { 
     printf(" Error, character must be a lowercase letter"); 
    } else 
    if (x < 97) { 
     printf(" Error, character must be a lowercase letter"); 
    } else 
    if (x == 116) { 
     printf("t is correct\n"); 

     printf("%d", word[0]); 
     printf(" _"); 
     printf(" _"); 
     printf(" %d ", word[3]); 

     e = getchar(); 

     if (e == 101) { 
      printf("e is correct\n"); 

      printf("%d", word[0]); 
      printf(" %d", word[1]); 
      printf(" _"); 
      printf(" %d ", word[3]); 
     } else 
     if (e == 115) { 
      printf("s is correct\n"); 

      printf("%d", word[0]); 
      printf(" _"); 
      printf(" %d", word[2]); 
      printf(" %d", word[0]); 
     } else { 
      printf(" You guessed wrong"); 
     } 
    } else 
    if (x == 101) { 
     printf("e is correct\n"); 
     printf("_"); 
     printf(" %d", word[1]); 
     printf(" _"); 
     printf(" _ "); 
    } else 
    if (x == 115) { 
     printf("s is correct\n"); 

     printf("_"); 
     printf(" _"); 
     printf(" %d", word[2]); 
     printf(" _ "); 
    } else { 
     printf(" You guessed wrong"); 
    } 
} 
+2

什麼是int(a);你的聲明對於初學者是不正確的...嘗試'int a;'也是'a == 0;'是'a'與零的比較,**不**爲* * a *賦值零,嘗試'a = 0;' –

+3

這是垃圾代碼。 –

+1

'c == 0;'等人。不要做任何事情。 – Biffen

回答

2

當你輸入你想getchar讀取一個字符,你用結束它進入關鍵吧?那輸入鍵將被放入輸入緩衝區stdin(這是什麼getchar讀取)作爲換行符,'\n'

這意味着對於您輸入的每個輸入,您實際上輸入了兩個個字符。

您需要跳過該換行符。這很容易只需添加第二getchar第一後,例如像做

x = getchar(); 
getchar(); // Read the newline from the Enter key 
+2

而'x'應該輸入'int',這樣你可以檢查if(x == EOF){return 1; }'在用戶取消輸入**之前**你的第二個'getchar'或者你將在第二個'getchar'上阻塞。 –

0

已經有一個很好的答案,但澄清 問題有點多,下面是一段簡單的代碼。 函數get_character將返回第一個輸入的字符 並消耗所有後續字符,直到下一個'\n'

如果一個字符是EOF,它不會阻止。

#include<stdio.h> 
int get_character(void); 

int main(void) 
{ 
     printf("character > "); 
     int c = get_character(); 
     while(c != EOF && c != 'q') 
     { 
       printf("You entered %c\n", c); 
       printf("character > "); 
       c = get_character(); 
     } 
} 

int get_character(void) 
{ 
     int c = getchar(), next_c; 
     if(c == EOF) 
     { 
       return EOF; 
     } 
     next_c = getchar(); // consume all \n 
     while(next_c != '\n' && next_c != EOF) 
     { 
       fprintf(stderr, "WARNING: consumed non-newline character: 0x%x\n", next_c); 
       next_c = getchar(); 
     } 
     return c; 
} 

順便說一句:你是能夠解決更快劊子手方式:

int get_character(void); 
#define MAX_TRIALS 3 
#define WORD_LEN 4 

int main(void) 
{ 
    char word[WORD_LEN + 1] = "test"; 
    char correct[WORD_LEN] = {0, 0, 0, 0}; 
    int trials = 0; 
    while(1) 
    { 
     if(trials > MAX_TRIALS) 
     { 
      printf("You failed. The word was: %s\n", word); 
      return 0; 
     } 

     printf("character > "); 
     int c = get_character(); 
     if(c == EOF) 
     { 
      printf("bye\n"); 
      return 0; 
     } 

     int i, this_char_correct = 0; 
     for(i = 0; i < WORD_LEN; i++) 
     { 
      if(c == word[i]) 
      { 
       correct[i] = 1; 
       this_char_correct = 1; 
      } 
      if(correct[i]) 
      { 
       printf("%c", word[i]); 
      } 
      else 
      { 
       printf("_"); 
      } 
     } 
     if(!this_char_correct) 
     { 
      trials++; 
     } 
     int word_done = 1; 
     for(i = 0; i < WORD_LEN; i++) 
     { 
      word_done &= correct[i]; 
     } 
     if(! word_done) 
     { 
      printf("\nYou have %d trials left\n", WORD_LEN - trials); 
     } 
     else 
     { 
      printf("\nYou got it.\n"); 
      return 0; 
     } 
    } 
} 

這只是一張證明了概念代碼有可能是解決這種方式更優雅的方式。