switch statement. simple to solve but i dunno whats wrong.

• 05-17-2012
switch statement. simple to solve but i dunno whats wrong.
what is happening here is it asks the first question and GREAT NEWS... YOU GET TO ANSWER THE QUESTION BUT THENNN..... the rest of the questions come up on the screen in great layout BUT... it wont let me answer QUESTION 2 WHICH HAS A SCANF IN IT SO I DONT KNOW WHY IT WONT LET THE USER INPUT THEIR ANSWER FOR THE SECOND QUESTION.
Code:

```#include <stdio.h> #include <stdlib.h> int main() {     int score = 0;     char answer;     printf("Question 1:\n How many times has Ross been married?\n a) twice\n b) once\n c) never\n d) three times\n your answer:");     scanf("%c",&answer);     switch (answer)     {     case 'a':score += 0;     break;     case 'b':score += 0;     break;     case 'c':score += 0;     break;     case 'd':score++;     break;     }     printf("\nQuestion 2:\n What are the names of pheobe's triplets?\n a) frank,leslie,chandler\n b) franks,chandler,mike\n c) leanne,chandler,lucy\n d) chandler,lucy,betty\n your answer:");     scanf("%c",&answer);     switch (answer)     {     case 'a':score++;     break;     case 'b':score += 0;     break;     case 'c':score += 0;     break;     case 'd':score += 0;     break;     }     printf("\n\nNow for questions about famous people.\n");     printf("Question 1:\n How many times has joan rivers been married?\n a) twice\n b) four times\n c) five times\n d) eight times jesus man\n ");     return 0; }```
• 05-17-2012
QuantumPete
Because when the user enters a<return>, the return is left in the buffer (since you only asked for a char. Easiest way to fix this is to add a space between the quotation mark and percent sign in your scanf:
Code:

`scanf (" %c", &answer);`
This swallows any whitespace (inclduing returns)
• 05-17-2012