-1

I've tried to make a trivia game. For some reason, after the first answer, the whole code prints itself when it's only supposed to print only one line at the time.

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;

}

Any ideas? :( BTW, i'm getting this error on my code:blocks

warning: character constant too long for its type warning: multi-character character constant wmultichar

got 6 warnings in total.

1 Answers1

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

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

You are confusing characters and strings. A string is an array of characters. %c is the scanf code for reading a single character; for a string you would use %s. Character literals are written with single quotes, like 'a'. String literals use double quotes: "A string". You can use the == operator to compare characters, but to compare strings, you should use the strcmp() function.

Your usage of the || operator doesn't do what you think it does. You need to write two separate tests:

if ( (answer == 'A') || (answer == 'a')) { /* etc */
Jim Lewis
  • 43,505
  • 7
  • 82
  • 96