What's wrong with my code? C -


i've tried make trivia game. reason, after first answer, whole code prints when it's supposed print 1 line @ time.

int main() 

{

    char answer;     int score = 16;       printf("hello , welcome chen's trivia!\n");     printf("press enter continue!\n");     getchar();      printf("ready? let's begin!\n");     printf("press enter continue!\n");     getchar();       printf(" president of united states?\n");     printf("barack obama, donald trump, george w. bush\n");     scanf(" %c", &answer);         if(answer == 'trump' || 'trump'){         printf("you 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 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 the main character in 'the matrix'? \n");     scanf(" %c", &answer);     if(answer == 'neo' || 'neo'){             score*=2;         printf("that's right!\n");         printf("your score %d\n", score);     }else{         score = (score / 2);     printf("sorry, wrong answer!\n");     }      printf("what 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 now:%d", score);     }   return 0; 

}

any ideas? :( btw, i'm getting error on code:blocks

warning: character constant long type warning: multi-character character constant wmultichar

got 6 warnings in total.

char answer; /* ... */ scanf(" %c", &answer);  if(answer == 'trump' || 'trump'){  /* et cetera */ 

you confusing characters , strings. string array of characters. %c scanf code reading single character; string use %s. character literals written single quotes, 'a'. string literals use double quotes: "a string". can use == operator compare characters, compare strings, should use strcmp() function.

your usage of || operator doesn't think does. need write 2 separate tests:

if ( (answer == 'a') || (answer == 'a')) { /* etc */ 

Comments

Popular posts from this blog

c# - Update a combobox from a presenter (MVP) -

How to understand 2 main() functions after using uftrace to profile the C++ program? -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -