2016-09-30 57 views
0

我有一個程序,我試圖運行。它編譯但沒有按預期運行(這部分只運行):程序沒有完全運行

#include <stdio.h> 

int main(int argc, const char * argv[]) { 

char play; 
char choice; 
char answer; 

printf("Welcome to Two doors.\n"); 
printf("Would you like to play? (y/n): "); 
scanf("%c", &play); 

if (play == 'y') { 

    printf("\nYou are a prisoner in a room with 2 doors and 2 guards.\n"); 
    printf("One of the doors will guide you to freedom and behind the other is a hangman --you don't know which is which.\n"); 
    printf("One of the guards always tells the truth and the other always lies. You don't know which one is the truth-teller or the liar either.\n"); 
    printf("You have to choose and open one of these doors, but you can only ask a single question to one of the guards.\n"); 
    printf("What do you ask so you can pick the door to freedom?\n\n"); 
    printf("\t1.Ask the truth-guard to point to the door of doom.\n"); 
    printf("\t2.Ask the liar-guard to point to the door of doom.\n"); 
    printf("\t3.Doesn't matter which one you pick.\n"); 
    scanf("%c", &choice); 

    char answer = "No matter which one you choose the guards both tell you which door leads to death, and therefore you can pick the other door.\n"; 
    switch (choice) { 
     case 1: 
      printf("%c", answer); 
      break; 
     case 2: 
      printf("%c", answer); 
      break; 
     case 3: 
      printf("%c", answer); 
      break; 
     default: 
      break; 
    } 
} 

return 1; 
} 

好像我無法讓代碼在其整體運行。這裏是我在輸入'y'後提問時得到的結果:

Welcome to Two doors. 
Would you like to play? (y/n): y 

You are a prisoner in a room with 2 doors and 2 guards. 
One of the doors will guide you to freedom and behind the other is a hangman --you don't know which is which. 
One of the guards always tells the truth and the other always lies. You don't know which one is the truth-teller or the liar either. 
You have to choose and open one of these doors, but you can only ask a single question to one of the guards. 
What do you ask so you can pick the door to freedom? 

    1.Ask the truth-guard to point to the door of doom. 
    2.Ask the liar-guard to point to the door of doom. 
    3.Doesn't matter which one you pick. 
(lldb) 

有沒有人可以幫助我?

謝謝!

+0

它究竟在哪停止? – Carcigenicate

+0

這是錯誤的。如果你不知道哪個警衛是騙子,你怎麼能問這個騙子後衛?這個問題不能很好地處理多種選擇,因爲一旦你知道解決方案是顯而易見的(向任何一個警衛詢問另一個**後衛會說什麼是自由之門)。 – Olaf

+0

重新提出您的問題:爲什麼忽略編譯器警告?如果您在忽略它們的情況下調用未定義的行爲,那麼遇到問題也就不足爲奇了。 – Olaf

回答

1
char answer = "No matter which one you choose the guards both tell you which door leads to death, and therefore you can pick the other door.\n"; 

這當然是錯誤的。 answer是一個char,只能保存一個字符而不是字符串。

您需要使用char *char []並用%s打印。

0

answer是一個char類型的變量,你不能給它分配一個字符串。

0
  1. answer需要類型const char *
  2. 變化switch (choice) { case 1:switch (choice) { case '1':
+0

如果你改變了'answer'的類型(你應該),那麼'printf(「%c」,answer);'調用就會失效。 '%c'將需要更改爲'%s'。 –

0

的問題是,當用戶按下ENTER鍵以表示他們想打他們實際上發送第二個字符。因此,choice設置爲\n

有幾件事情需要改變:

  1. char answer = "... - >char answer[] = "...
  2. 在開關接通'周圍的數字(例如1 - >'1'
  3. 跳過在選擇換行。

我對3號首先想到的是這樣的:

do{ 
    scanf("%c", &choice); 
} while(choice != '1' && choice != '2' && choice != '3'); 
0

switch節的printf()語句丟失換行符(\n)。根據您的終端,這可能會導致輸出被隱藏。使用:printf("%c\n", answer);