2017-04-07 91 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 */