2014-10-09 65 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