2015-06-14 38 views
0

我在YouTube上跟隨一個教程,並且正在做一個骰子生成器。 它基本打印出3個骰子的結果並且總結出骰子的結果。 之後,用戶將查看總和,並且基於總和,用戶將猜測下一卷將會更高,更低還是相同。生成一個骰子游戲 - C編程

下面是我的代碼,假設,當我輸入'yes'時,它應該執行if語句中的代碼。然而,它直接去了else語句。有人可以告訴我有什麼問題嗎?

int answer; 
int guess; 
int diceRoll4 = 0; 
printf("Would you like to guess your next dice? Y/N \n"); 
scanf(" %c", &answer); 

if (answer == 'yes'){ 

    printf("What is your guess?\n"); 
    printf("please key in your number \n"); 
    scanf(" %d", &guess); 
    if (guess > diceRoll4){ 
     printf(" You got it wrong, too high!"); 
    } 
    else if (guess < diceRoll4){ 
      printf(" You got it wrong, too low!"); 
    } 
    else { 
     printf("You got it right"); 
    } 

} 
else{ 
    printf("Thanks for playing"); 
} 
+0

除其他事項外,'的scanf(......%c..'讀取單個字符.. – Sathya

+0

張貼的代碼編譯都不也不是完整的程序。請發佈具有運行問題的代碼時。郵政編碼,乾淨地編譯和顯示問題。 – user3629249

回答

4

首先,answer應該是一個數組char s爲了容納一個字符串。更改

int answer; 

char answer[10]; //Or any other reasonable size 

其次,由於要掃描的字符串,而不是一個字符,改變

scanf(" %c", &answer); 

scanf("%9s", answer); 

的9將掃描最多9個字符(NU爲+1 L末端),從而阻止buffer overflows
我已經刪除&作爲%s預計char*&answer將給出char(*)[10]。數組的名稱被轉換爲指向其第一個元素char*的指針,正是%s所期望的。上述scanf因此等於

scanf("%9s", &answer[0]); 

第三,比較使用==比較指針,而不是在它們的實際內容兩個字符串。改爲使用strcmp而不是string.h。當它的兩個參數擁有相同的內容時,它將返回0。更改

if (answer == 'yes'){ 

if (strcmp(answer, "yes") == 0){ 

雙引號用來表示一個NULL結尾的字符串(char*),而這正是strcmp預期,而單引號,因爲在你的代碼,是一個多 - 字符文字,其值是實現定義的。

1

要測試相等性,您必須使用strcmp。如果返回值爲0這意味着它們相等。

if (strcmp(answer, "yes") == 0) { 
    // ... 
} else { 
    // ... 
} 

注:

  1. 只需使用answer == 'yes'來測試指針沒有價值的平等。這就是爲什麼只進入else的原因。

  2. 因爲answerint你必須改變到一個數組

    char answer[15] 
    
  3. 正如@Sathya提到你正在閱讀只是一個char %c閱讀,你必須使用%s

    scanf("%s", answer); 
    
  4. 字符串
  5. 而不是'yes'這是多字符字符常量更改爲"yes"這是一個陣列char\0在最後,more informations here

4

'yes'是一個多字節字符,其行爲是實現定義的。

你可能想要的是閱讀和比較單一的char

if (answer == 'y'){ 

或讀一整串和比較:

char answer[128]; 
scanf("%s", answer); 
if (strcmp(answer,"yes") == 0){ 
... 
} 

注意,我改變了answer和使用%s來閱讀字符串

2

如果你不想在一個字符串閱讀,但只有一個char,用戶可以回答任何YN,你應該改變int answer;char answer;。然後,您可以繼續使用原來的scanf()-呼叫。您仍然需要改變

if (answer == 'yes') 

if (answer == 'Y') 

如果你希望用戶直接鍵入yY你可以用戶toupper()ctype.h和改變你的if -condition到if (toupper(answer) == 'Y')

0

這一行:

如果(答案== '是'){

有幾個問題。

1) the definition of 'answer' is 'int' but the scanf is inputting a single character 

2) answer could be compared with 'y' or 'n' but not to a array of char. 

3) since the scanf only input a single char 
    and you/the user input 'yes', 
    only the first character was consumed, 
    so the 'es' are still in the input buffer 

4) note the the single character could be anything, except white space. 
    the leading space in the format string would consume any white space. 
    so the user could input say 'y' or 'Y' 
    these are different characters 
    however, using the toupper() macro from ctypes.h 
    would mean only a 'Y' would need to be compared 

5) if you decide to read a string, 
    then 'answer' needs to be a character array, 
    say: char answer[10]; 
    and the scanf needs to have a max length modifier 
    on the associated "%s" input/conversion parameter 
    so as to avoid the user overflowing the input buffer 
    and the comparison would be via the strcmp() function 

6) always check the returned value (not the parameter value) 
    from scanf to assure the operation was successful 

7) diceRoll4 and guess can never be a negative number 
    so the variable definitions should be unsigned 
    and the associated scanf() for guess should use 
    something like "%u" 

8) on the printf() format strings, always end them with '\n' 
    so the sting will be immediately displayed to the user, 
    otherwise, they will only be displayed 
    when a input statement is executed or the program exits