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

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    99

    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;
    }

  2. #2
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    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)
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    99
    nice one mate, appreciate the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. whats wrong with this very simple code?
    By sway1 in forum C++ Programming
    Replies: 2
    Last Post: 07-06-2008, 12:18 PM
  2. Whats wrong with this simple code?
    By o14v in forum C++ Programming
    Replies: 4
    Last Post: 03-19-2008, 01:46 PM
  3. Whats wrong with this statement?
    By lawina in forum C++ Programming
    Replies: 7
    Last Post: 05-25-2006, 11:06 AM
  4. Whats Wrong in My simple program?
    By MartinLiao in forum C Programming
    Replies: 1
    Last Post: 09-02-2004, 02:30 AM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 12:51 AM