2014-10-09 3 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"); 

... 편집 : 그것은 나를 네 번째 입력에 넣어 수 없습니다. enter를 치면, 반환 된 프로세스가 표시됩니다. 당신은 그러나 실제로는 두 개의 문자를 입력하기 위해 당신이 문자를 입력, 단일 문자를 읽도록 요구하고있다

scanf(" %c", &swordch0c1); 

: 를 입력

+0

네 번째 입력을 입력 하시겠습니까? 아니면 그냥 건너 뛰시겠습니까? –

+0

전체 코드를 표시해야합니다 (또는 적어도 "네 번째 사용자 입력"시점까지). – jwodder

+0

@jwodder, 그것은 네 번째 입력까지 코드입니다. – Jeremy

답변

0

이 라인은 거의 확실히 문제입니다. 이 scanf 호출은 'A'을 읽고 다음에 scanf 호출은 여전히 ​​입력 버퍼에있는 '\n' (입력) 키를 읽습니다.

모든 사용자 입력에 fgets()을 사용하는 것이 좋습니다. 실제로 사용자가 실제로 입력하는 방식 (텍스트 행에 입력)과 일치하기 때문에 이러한 방식으로 처리하는 것이 훨씬 쉽습니다.

+0

대단히 고마워요. 왜 사용자가 그 형식으로 작업하기 전에 입력했는지에 대한 아이디어가 있습니까? – Jeremy

+0

앞 (' "% c"')의 공백은'scanf'가 입력 버퍼의 공백을 뛰어 넘을 것입니다 (Enter keypress는 공백 임). 네 번째 사용자 입력을 표시하지 않았으므로 그 내용이 확실하지 않습니다. –

관련 문제