Thread: what am i missing?

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    9

    what am i missing?

    Hi, I'm making this simple program but i'm having a problem somewhere in Question 2. It doesn't prompt the user make an input. It is only the part where it does not prompt for user input.

    Code:
    #include <stdio.h>
    main()
    {
             char answer1,answer2,answer3;
          
             printf("*** QUIZ *** \n\n");
             printf("Enter the letter of the correct answer.\n\n");
             printf("Question 1: Choose the correct spelling.\n\n");
             printf("\tA) Mississippi , B) Missisippi , C) Misissippi = ");
             scanf("%c", &answer1);
             if (answer1 == 'a' || answer1 == 'A')
                  {   printf("\nCorrect!\n\n");
          }
             else if (answer1 == 'b' || answer1 == 'B')
                    {  printf("Wrong!\n\n");
                           }
             else if (answer1 == 'c' || answer1 == 'C')
                     {  printf("Wrong!\n\n");
                           }
             else
                    {      printf("Invalid input!\n\n");
                           }
             printf("Question 2: How many sides does a triangle have?\n\n");
             printf("\tA) 33 , B) 3 , C) 13 = ");
             scanf("%c", &answer2);
             if (answer2 == 'a' || answer2 == 'A')
                   {   printf("\nWrong!\n\n");
                       }
             else if (answer2 == 'b' || answer2 == 'B')
                     {  printf("Correct!\n\n");
                           }
       else if (answer2 == 'c' || answer2 == 'C')
                     {  printf("Wrong!\n\n");
                           }
             else
                     {      printf("Invalid input!\n\n");
                            }
             printf("Question 3: 1000 + 1 = ?\n\n");
             printf("\tA) 1100 , B) 1011 , C) 1001 = ");
             scanf("%c", &answer3);
             if (answer3 == 'a' || answer3 == 'A')
                    {   printf("\nWrong!\n\n");
                        }
             else if (answer3 == 'b' || answer3 == 'B') 
                      {  printf("Wrong!\n\n");
                             }
              else if (answer3 == 'c' || answer3 == 'C') 
                       {  printf("Correct!\n\n");
                             }
             else
                      {      printf("Invalid input!\n\n");
                             }
          
              system("pause");
    }
    =============
    ** this is what it shows when I tried to run it:

    *** QUIZ ***

    Enter the letter of the correct answer.

    Question 1: Choose the correct spelling.

    A) Mississippi , B) Missisippi , C) Misissippi = a

    Correct!

    Question 2: How many sides does a triangle have?

    A) 33 , B) 3 , C) 13 = Invalid input!

    Question 3: 1000 + 1 = ?

    A) 1100 , B) 1011 , C) 1001 = v
    Invalid input!

    Press any key to continue . . .

    ============

    thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Step 1, learn how to indent code.
    Code:
    #include <stdio.h>
    main()
    {
      char answer1, answer2, answer3;
    
      printf("*** QUIZ *** \n\n");
    
      printf("Enter the letter of the correct answer.\n\n");
      printf("Question 1: Choose the correct spelling.\n\n");
      printf("\tA) Mississippi , B) Missisippi , C) Misissippi = ");
      scanf("%c", &answer1);
      if (answer1 == 'a' || answer1 == 'A') {
        printf("\nCorrect!\n\n");
      } else if (answer1 == 'b' || answer1 == 'B') {
        printf("Wrong!\n\n");
      } else if (answer1 == 'c' || answer1 == 'C') {
        printf("Wrong!\n\n");
      } else {
        printf("Invalid input!\n\n");
      }
    
      printf("Question 2: How many sides does a triangle have?\n\n");
      printf("\tA) 33 , B) 3 , C) 13 = ");
      scanf("%c", &answer2);
      if (answer2 == 'a' || answer2 == 'A') {
        printf("\nWrong!\n\n");
      } else if (answer2 == 'b' || answer2 == 'B') {
        printf("Correct!\n\n");
      } else if (answer2 == 'c' || answer2 == 'C') {
        printf("Wrong!\n\n");
      } else {
        printf("Invalid input!\n\n");
      }
    
      printf("Question 3: 1000 + 1 = ?\n\n");
      printf("\tA) 1100 , B) 1011 , C) 1001 = ");
      scanf("%c", &answer3);
      if (answer3 == 'a' || answer3 == 'A') {
        printf("\nWrong!\n\n");
      } else if (answer3 == 'b' || answer3 == 'B') {
        printf("Wrong!\n\n");
      } else if (answer3 == 'c' || answer3 == 'C') {
        printf("Correct!\n\n");
      } else {
        printf("Invalid input!\n\n");
      }
    
      system("pause");
    }
    Next, you have to understand that %c conversion format takes the next character unconditionally (most of them skip leading white space).
    So in your code, the second %c is really reading the newline from the previous question.

    To test this, just type in
    abc
    and press enter.

    To fix the problem, change the scanfs to
    scanf(" %c", &answer2);
    and
    scanf(" %c", &answer3);
    Note the leading space in the format.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User dariyoosh's Avatar
    Join Date
    Nov 2012
    Location
    Iran / France
    Posts
    38
    Hello there,

    You're question has already been answered here

    C: Mutliple scanf's, when I enter in a value for one scanf it skips the second scanf. - Stack Overflow

    An immediate solution would be to add a getchar() after each scanf() call.

    Also, as it seems to me you need to add #include <stdlib.h> at the beginning of your code.


    [Edit]:
    But now that I'm reading Salem's solution after posting this, of course his solution is much simpler


    Regards,
    Dariyoosh
    Last edited by dariyoosh; 11-22-2012 at 02:47 AM.

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    9
    @Salem
    Hi, sorry about the indention, and thank you, helps a lot.

    @dariyoosh
    Thank you too.

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    If your input is line-by-line based, then an often overlooked approach is to first read in a line and then use sscanf on the result.

    Code:
    #include <stdio.h>
    
    char buf[BUFSIZ];
    
    int main()
    {
        char c='?';
        fgets(buf, BUFSIZ, stdin);
        sscanf(buf, " %c ", &c);
        printf("You entered the character '%c'\n", c);
        return 0;
    }
    This approach requires a little more work but to me seems a lot less error prone, because you always can restrict your attention to a single line of input. Also error handling with user input is simplified because you can check the return result of sscanf and ask the user to re-enter that line, for example. Or if the input is a file you can print out which line number the error occured on.

  6. #6
    Registered User
    Join Date
    Nov 2012
    Posts
    4
    use fflush(stdin); right before each scanf when using characters

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    4
    Im completely new to programming. i hope newbies are welcome. ahah
    Up until the beginning of the semester(3 months ago) i had no idea what "c" was and now im able to write simple codes. My teacher tells us to use fflush all the time :S so tomorrow im going to bring up this topic to her.

  9. #9
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Quote Originally Posted by iSee View Post
    Im completely new to programming. i hope newbies are welcome...

    Yes they are


    Don't be offended by Salem's bluntness - A good answer is factually correct and void of any emotion.


    You had a solution that you believe was correct and you shared it - That was the right thing to do.


    Salem saw that your answer was not entirely correct and shared it with you - Now everyone is better off


    I hope that you don't take the void of emotion as being unwelcoming (a mistake that a lot of new posters make).
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help, what am I missing
    By neocuel in forum C Programming
    Replies: 14
    Last Post: 01-20-2010, 11:12 AM
  2. Am I missing something?
    By ke121885 in forum C Programming
    Replies: 8
    Last Post: 10-11-2009, 09:45 PM
  3. what am i missing now...
    By MK27 in forum C Programming
    Replies: 8
    Last Post: 09-15-2008, 03:13 AM
  4. Something I'm Missing...
    By Dukefrukem in forum C Programming
    Replies: 2
    Last Post: 07-09-2008, 01:47 PM
  5. Missing something
    By campermama in forum C++ Programming
    Replies: 5
    Last Post: 07-11-2004, 11:43 PM