2017-04-07 1 views
-1

퀴즈 게임을 만들려고했습니다. 어떤 이유로, 첫 번째 대답 이후에 전체 코드는 그 때 한 줄만 인쇄 할 때 인쇄됩니다.내 코드에 어떤 문제가 있습니까? C

int main() 

{

char answer; 
    int score = 16; 


    printf("Hello and welcome to Chen's trivia!\n"); 
    printf("Press ENTER to continue!\n"); 
    getchar(); 

    printf("Ready? Let's begin!\n"); 
    printf("Press ENTER to continue!\n"); 
    getchar(); 


    printf(" Who is the president of the united states?\n"); 
    printf("Barack Obama, Donald Trump, George w. Bush\n"); 
    scanf(" %c", &answer); 
     if(answer == 'Trump' || 'trump'){ 
     printf("You are correct\n"); 
     score*=2; 
     printf("your score is: %d \n", score); 
    }else{ 
     score = (score/2); 
    printf("Wrong answer!\n"); 
    printf("score: %d \n", score); 
    } 


    printf("What superhero shoots web out of his arms?\n"); 
    printf("A.Batman, B.Spiderman, C.Captain America, D.Superman\n"); 
    scanf(" %c", &answer); 
    if(answer == 'B' || 'b'){ 
     printf("That's right, Hero!\n"); 
     score*=2; 
     printf("Youre score is: %d \n", score); 
    }else{ 
    score = (score/2); 
    printf("sorry, wrong answer!\n"); 
    printf("your score is! %d\n", score); 
    } 

    printf("Who is the the main character in 'The Matrix'? \n"); 
    scanf(" %c", &answer); 
    if(answer == 'neo' || 'NEO'){ 
      score*=2; 
     printf("That's right!\n"); 
     printf("Your score is %d\n", score); 
    }else{ 
     score = (score/2); 
    printf("Sorry, Wrong answer!\n"); 
    } 

    printf("What is the capital of Israel?"); 
    scanf(" %c", &answer); 
    if(answer== ('jerusalem') || ('Jerusalem')){ 
      score*=2; 
     printf("That's right!"); 
     printf("Your score is:%d", score); 
    }else{ 
     score = (score/2); 
    printf("Sorry, wrong answer!"); 
    printf("Your score is now:%d", score); 
    } 


return 0; 

}

어떤 아이디어가? :( 은 BTW, 난 내 코드에서이 오류를 받고 있어요 : 블록

경고 : 해당 유형 경고에 대한 문자 상수 너무 오래 :. 멀티 문자 문자 상수 wmultichar 총 6 경고를 받았습니다

+0

의 사용 가능한 복제 (http://stackoverflow.com/questions/8004237/how-do-i-properly-compare -strings-in-c) –

+0

또한 C에서 문자열 리터럴을 작은 따옴표로 묶지 마십시오. –

+0

행 읽기 ......... –

답변

1
char answer; 
/* ... */ 
scanf(" %c", &answer); 

if(answer == 'Trump' || 'trump'){ /* et cetera */ 

당신은 혼란 문자와 문자열 문자열은 문자의 배열입니다 %c는 단일 문자를 읽는는 scanf 코드입니다... 문자열이 %s을 사용 에 대한 문자 litera ls는 작은 따옴표 (예 : 'a')로 작성됩니다. 문자열 리터럴은 큰 따옴표를 사용합니다 ("A string"). == 연산자를 사용하여 문자를 비교할 수 있지만 문자열을 비교하려면 strcmp() 함수를 사용해야합니다.

|| 연산자를 사용하면 생각했던대로 작동하지 않습니다. 당신은 두 개의 별도의 테스트를 작성해야합니다 [? 내가 제대로 C에서 문자열을 비교하려면 어떻게]

if ((answer == 'A') || (answer == 'a')) { /* etc */ 
관련 문제