2014-10-09 107 views
-4

我真的不知道我在做什麼錯了。每次我編譯它時,在第四個用戶輸入處,它就停止並顯示「進程返回」的東西。我在做什麼錯? (C)

#include <stdio.h> 
#include <conio.h> 

int main() { 

char firstname[15], class, swordch0c1, swordch0c2; 
int health, healthtot, armor, armortot; 

printf("Hello there! Could I have your first name?\n>"); 
scanf("%s", firstname); 

printf("\n---------------------The Legend of %s---------------------", firstname); 
printf("\nPress Enter to continue."); 
getch(); 

printf("\n\n\nYou are %s, a(n): \nA.Swordsman\nB.Assassin\nC.Archer\nD.Mage\n>", firstname); 
scanf(" %c", &class); 

/*swordsman story starts here*/ 
if (class=='a' || class=='A') 
    { 
    printf("\n\nThere you stand, at your boring everyday post.\nWhen you joined the army, you thought it would be more exciting than this.\nJust then, you see your general walking towards you."); 
    printf("\n\nYou quickly improve your posture. \"Soldier, I have an opportunity for you\"\nA.\"Really? What is it?\"\nB.\"I'm not interested\"\n>"); 
    scanf(" %c", &swordch0c1); 

    if (swordch0c1=='b'||swordch0c1=='B') 
     { 
     printf("\n\"But... I didn't even tell you what it was. Okay, suit yourself\" You are DOOMED to a life of boredom.\n\n\n\n\n"); 
     } 

    if (swordch0c1=='a'||swordch0c1=='A') 
     { 
     printf("\n\n\n\"Well, you see, there's this dragon. He's been causing big problems.\nHe's destroyed villages, harrassed the priests on the mountain,\n"); 

... 編輯:它允許我把第四輸入。在進入後,它顯示進程返回的東西。

+0

你可以輸入第四個輸入嗎?還是隻是跳過它? – 2014-10-09 00:31:00

+0

你將不得不展示你的整個代碼(或者至少達到「第四個用戶輸入」的點)。 – jwodder 2014-10-09 00:31:32

+0

@jwodder,這是直到第四個輸入的代碼。 – Jeremy 2014-10-09 00:33:28

回答

0

這條線幾乎是肯定的問題:

scanf(" %c", &swordch0c1); 

你問到讀單個字符,但實際上進入你有一個字符輸入兩個字符:一個輸入。此scanf調用將讀取'A',並且下一個scanf調用將讀取仍在輸入緩衝區中的'\n'回車)密鑰。

我建議使用fgets()所有的用戶輸入。處理這種方式要容易得多,因爲它符合用戶實際輸入輸入的方式(文本行加上輸入)。

+0

非常感謝。你有一個想法,爲什麼用戶輸入之前,它的格式? – Jeremy 2014-10-09 00:36:00

+0

前面的空格('「%c」')將導致'scanf'跳過輸入緩衝區中的空格(Enter鍵按空格)。你沒有顯示你的第四個用戶輸入,所以我不確定它說了什麼。 – 2014-10-09 00:37:19