2014-08-28 47 views
1

首先,我只想說我2周前沒有聽說過c,如果這給你一些想法我在哪裏。無論如何,我正在努力編寫我的第一個程序,允許用戶做出選擇並繼續講故事。在這裏,我只會告訴你的代碼:scanf(「%c」,x)和x = getchar都不在等待輸入

#include <stdio.h> 

//Global variables for entire program 

char proceed; //Used to confirm that user wishes to play 
int countdwn=5; //Used in do-while loop to count down from red value 
char xchoice; //Used in ABC Multiple Choice Selections 

int main() 
{ 
//Introduction sequence displays a block of text for user. 

    puts("\nGreetings,\n"); 
    puts("I am glad that you made it this far!"); 
    puts("Now, you will discover if it was worth your while!"); 
    puts("In this program you will be provided with some information"); 
    puts("which you must use to make wise decisions..."); 
    puts("Whether you survive or not remains to be seen"); 
    puts("But whatever your outcome, you are soley responsible"); 
    puts("as you are the one making the choices... for good or ill!\n\n"); 

//In order for the user to continue, user must type a '~' followed by enter. 

    puts("Are you ready to begin!?!?"); 
    puts("If so press \"~\" followed by enter/return---"); 
    proceed=getchar(); 

    if(proceed!='~') 
    { 
      puts("\nIt seems that you don't want to continue."); 
      puts("The program will now close and return you to dos"); 
      puts("Once the program has closed the cursor will begin to"); 
      puts("blink again and then type \"exit\" to exit dos"); 
      sleep(7); 
      puts("Program Closing in 5..."); 

    do 
    { 
      printf("%d\n", countdwn); 
      sleep(1); 
      countdwn--; 
    } 
    while(countdwn>0); 
    return 0; 
    } 

/*************************************************************************/ 

    puts("\nYou are living in Japan during a time known as the"); 
    puts("\"Sengoku Jidia,\" or age of the warring states."); 
    puts("You are a Ninja --- a secret, deadly warrior."); 
    puts("What kind of missions will you take on?"); 
    puts("What kind of dangers will you face?"); 
    puts("Keep going to find out!\n"); 
    puts("Make a selection by typing the number associated"); 
    puts("with the selection you wish to make, followed by hitting enter."); 
    puts("Then, the next choice will automatically appear below.\n"); 
    puts("a. To help in the attack on an enemy castle in 1558"); 
    puts("b. To defend your homeland from an enemy attack in 1581"); 
    puts("c. To serve as a bodygard to a powerful ruler in 1600"); 
    puts("Make your selection a, b, or c followed by enter."); 

    scanf("%c", &xchoice); 

    if(xchoice=='A' || 'a') 
    { 
      puts("\nYou stand alone, looking up at the walls of"); 
      puts("Sawayama Castle near the city of Hikone."); 
      puts("Activity buzzes all around you. You're a mercenary"); 
      puts("ninja, fighting for whomever pays you the most for your"); 
      puts("services. Today, you're part of an army fighting under"); 
      puts("Rokkaku Yoshita the samurai leader of the Rokkaku clan."); 
      puts("The dodo clan is supposed be your ally, but some have"); 
      puts("rebelled and taken control of one of your castles."); 
      puts("You have no choice but to fight to regain your loss.\n"); 
      puts("You are one of 50 ninjas hired to take part in the"); 
      puts("seige. Your leader, Doshun, is already forming plans"); 
      puts("on how to get inside. Doshun is a clever man and a"); 
      puts("respected ninja. But as night approaches, you can't"); 
      puts("help feeling that the time to strike is now.\n\n"); 
      puts("You stare at the castle wall. You know you could get"); 
      puts("inside. Then you could spy on the enemy or set fires"); 
      puts("that would drive the enemy leader, Kuranosuke from"); 
      puts("hiding. But Doshun is your leader. He will have a plan,"); 
      puts("and it might be best to find out what it is.\n\n"); 
      puts("a. To try to get inside the castle walls alone."); 
      puts("b. To wait for Dashun's plan."); 

/*****************************************************************************/ 

      } 


    return(0); 
} 

這是我至今寫的,但是我打算用更多的選擇,繼續吧。你幾乎可以看到它要去的地方。該程序編譯時沒有任何警告或錯誤。倒計時的do-while循環和這樣的工作很好。但是,如果我做按波浪號〜鍵,然後按回車,而不是從

scanf("%c", &xchoice); 

等待用戶輸入的程序簡單地停止運行,終端只是追溯到顯示我的當前目錄。 (我正在爲Windows編寫Linux)。這顯然不是預期的結果。此外,我也嘗試插入一個循環,使getchar工作,直到輸入被按下,並沒有工作。我也嘗試使用getchar以與上面的scanf語句相同的方式讀取單個字符,但結果相同。我相信我錯過了一些明顯的東西......但我無法找到它。最後,我想知道是否有更好的方法在屏幕上顯示大量文本(「djfasjlaksdj」);

非常感謝!

+0

你試過'scanf(「%c」,&xchoice);'? – haccks 2014-08-28 12:18:44

+3

'xchoice =='A'|| 'a''總是如此。你應該提高你的警告級別,因爲我得到了一些很好的警告級別:*隱式聲明函數'sleep'在C99 *中是無效的並且*使用邏輯'||'與恆定操作數* – chris 2014-08-28 12:20:01

+0

爲什麼克里斯?只是好奇...它不區分大小寫嗎?好吧,我會試試看,因爲當我做到這一點時沒有任何警告...... – 2014-08-28 12:20:58

回答

0

輸入完畢後,按ENTER鍵或打印輸入字符串後,如果兩個字符放在輸入緩衝區中,則它們是:輸入字符和換行符由於輸入鍵。輸入的字符被getchar()消耗,但換行符保留在輸入緩衝區中。所以\n被下一個scanf()消耗。爲了避免一個空間,嘗試前%c -

scanf(" %c", &xchoice); // Fix 1 
    ^<-Note the space here 

if(xchoice=='A' || 'a') 

評估總是如此。怎麼樣?您只在A檢查輸入。不適用於a。所以如果xchoice=='A'失敗意味着,'a'始終保持爲真。因此,嘗試

if(xchoice == 'A' || xchoice == 'a')) // Fix 2. 
+0

剛剛重新編譯成功,謝謝一堆:-) – 2014-08-28 12:30:21

+0

雖然這解決了這個問題,但它並不能解釋爲什麼這些更改是必要的。 – dreamlax 2014-08-28 12:31:42

1

getchar()scanf小號讀入一個字符並停止stdin閱讀,但是當你輸入一個選項,發送至少2個字符:您鍵入的選項和\n。您的代碼只讀取您的選項,並保留第二個電話的\n

爲避免讀取行尾或任何其他空格或製表符,應使用scanf(" %c", &xchoice)(注意%c之前的空格)。

而且,還有一點:你行:

if(xchoice=='A' || 'a') 

不能按預期工作。你應該改變它:

if(xchoice=='A' || xchoice=='a') 
3

getchar函數爲標準輸入讀取單個字符。不幸的是,終端將不會發送任何字符到程序,直到您按下Enter ...所以這就是爲什麼您需要用戶在〜getchar之後鍵入ENTER才能返回。然而,ENTER不被getchar讀取,但被隨後的輸入讀取。

因爲我們有一個面向行的用戶界面,所以最好的辦法是始終讀取整行,然後將其解釋爲單個字符,命令或任何其他內容。讀取單行而不冒上溢風險的最佳選擇是使用fgets函數。

附錄。 在我看來,使用scanf(" %c"...)的建議並不好。圖片如果用戶寫下「你好嗎?」這樣的字符串會發生什麼?當你問一個字符...

0

只是scanf函數

fflush(stdin); 

好走之前刷新輸入緩衝區。