Thread: Compiler keeps skipping over the scanf statement!! Please Help.

  1. #1
    Registered User
    Join Date
    Nov 2011
    Location
    Canada
    Posts
    22

    Compiler keeps skipping over the scanf statement!! Please Help.

    Hello, I am trying to code a basic program which takes 2 inputs from a user based on a menu and then converts the second input to the menu choice chosen. For example, if 'a' is chosen and the number '5' was inputted then there would be a print stating that 5 is a digit.

    Here is the code:

    Code:
    bool isDigit(char num)
    {
         if(num>='0' && num<='9')
         {
                   return true;
         }    
    }
    
    int main (void)
    {
    char user1 = 0; // declares a character variable for the user inputted function
    char ch; // delcares a character variable for the user inputted character
    
    printf ("*****************************************" // Displays the menu system
              "\n* Character Arithmetic      *" // the title
              "\n*****************************************"
              "\n* Program instructions here *" // short instructions
              "\n* Program instructions here *" // short instructions
              "\n*****************************************"
              "\n* Functions: *" // displays the 5 functions
              "\n* a. isDigit *"
              "\n* b. isLetter *"
              "\n* c. toUpper *"
              "\n* d. toLower *"
              "\n* q. Quit *"
              "\n*****************************************");
    
      printf (" Choose a function: "); // prompts the user to enter a function    
      scanf ("%c", &user1); // saves the user input as 'user1'
      printf ("Enter a character you would like to use the function on: \n"); // asks the user to input a character
      scanf ("%c", &ch); // saves the user inputted character as 'ch'
      
      if ( user1 == 'a' && isDigit(ch) )
      {
           printf ("%c is a digit!\n", ch);
      }
    }

    the problenm occurs when the it is supposed to ask me for my second input (ch) but it seems to just ask me for my input but not scan for it and assign it to ch instead it just exists! I can't seem to find anything wrong.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    scanf() has a problem with %c. Since you are expecting just a char, and the newline left behind by scanf() always, is a char, it will pull off one newline char '\n', from the keyboard buffer, and seem to "skip" the input completely.

    In reality, it didn't skip anything. It took the newline, thought that was what you wanted, and continued.

    Add a getchar() after each scanf(), to pull the newlines off the keyboard buffer.

    #1 complaint by C learners. A space between the " %, may do the same thing.

    And Welcome to the Forum, SimarGill!

  3. #3
    Registered User
    Join Date
    Nov 2011
    Location
    Canada
    Posts
    22
    Thanks Adak, I just placed a space between " % and it worked perfectly. Still don't understand the whole '\n' and keyboard buffer concept. But I appreciate your help. Thanks though and see you around.

  4. #4
    Team Bring It rajarshi's Avatar
    Join Date
    Nov 2011
    Location
    India
    Posts
    79
    Quote Originally Posted by SimarGill View Post
    Thanks Adak, I just placed a space between " % and it worked perfectly. Still don't understand the whole '\n' and keyboard buffer concept. But I appreciate your help. Thanks though and see you around.

    scanf() does assigns a char/int/float to appropriate variables and keeps the enter key unread in the keyboard buffer.
    getchar() will read the enter key from the buffer thinking the user has entered the enter key .

    " I failed in some subjects in exam , but my friend passed in all . Now he is an engineer in Microsoft and I am the owner of Microsoft !! "

    - Bill Gates .

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by SimarGill View Post
    Thanks Adak, I just placed a space between " % and it worked perfectly. Still don't understand the whole '\n' and keyboard buffer concept. But I appreciate your help. Thanks though and see you around.
    When you made an entry, how many keys did you press?

    You pressed 2 keys... the character and the enter key...
    scanf() consumed the character you were asking for.
    The enter key (\n) remains in the keyboard buffer.
    Along comes another scanf() that's only looking for 1 character.
    It finds the enter key and keeps on trucking.

    When you add the space as in scanf(" %c"... you are telling it to ignore invisible characters and wait for the next keypress... which will, no surprises, put 2 more characters in the buffer.


    I (personally) view this as a nasty flaw in C's input routines... but it is what it is and we get to live with it.

  6. #6
    Team Bring It rajarshi's Avatar
    Join Date
    Nov 2011
    Location
    India
    Posts
    79
    Quote Originally Posted by CommonTater View Post



    I (personally) view this as a nasty flaw in C's input routines... but it is what it is and we get to live with it.
    LOL

    " I failed in some subjects in exam , but my friend passed in all . Now he is an engineer in Microsoft and I am the owner of Microsoft !! "

    - Bill Gates .

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by rajarshi View Post
    LOL
    OK... why do you think that's funny?

    I've done a lot of programming in my days, now about half and half Pascal and C and I can tell you C's console input functions massively suck. As in, it's a real problem, just look how many times we get asked bout this each week.

  8. #8
    Team Bring It rajarshi's Avatar
    Join Date
    Nov 2011
    Location
    India
    Posts
    79
    Quote Originally Posted by CommonTater View Post
    OK... why do you think that's funny?

    I've done a lot of programming in my days, now about half and half Pascal and C and I can tell you C's console input functions massively suck. As in, it's a real problem, just look how many times we get asked bout this each week.
    funny cuz...we got to live with it..and we can do absolutely nothing about it...
    another flaw which I feel is (personal opinion)...operation between an integer and real always yields a real result...(no matter what we do..the integer is always promoted to real and we get a real result)

    " I failed in some subjects in exam , but my friend passed in all . Now he is an engineer in Microsoft and I am the owner of Microsoft !! "

    - Bill Gates .

  9. #9
    Registered User
    Join Date
    Nov 2011
    Location
    Canada
    Posts
    22
    LOL ok i got it now, so im basically telling scanf to wait for 2 characters in the buffer since the enter key seems to skip it. Wow, that is a flaw.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by SimarGill View Post
    LOL ok i got it now, so im basically telling scanf to wait for 2 characters in the buffer since the enter key seems to skip it. Wow, that is a flaw.
    Nope, just the opposite... %c tells scanf() to wait for 1 character, but you are entering 2... the character itself and the enter key both end up in the buffer.

  11. #11
    Registered User
    Join Date
    Nov 2011
    Location
    Canada
    Posts
    22
    Ah I got it now. Thanks but in that case, %c can be %d or whatever since it will always be waiting for one sort of input. Or is this issue only with %c. I never had this before.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by SimarGill View Post
    Ah I got it now. Thanks but in that case, %c can be %d or whatever since it will always be waiting for one sort of input. Or is this issue only with %c. I never had this before.
    If you use %d scanf() will try to write sizeof(int) bytes... which may not be what you want.

    It's an issue with %c and %s ... Look up scanf() in your compiler's library documentation (if you don't have it, get it) it should explain all this for you.

    Trust me, you will keep banging into this over and over again.

  13. #13
    Registered User
    Join Date
    Nov 2011
    Location
    Canada
    Posts
    22
    Ohh alright I got it. In my book, it just that says that scanf reads an input from stdin stream. The string pointed to by format specifies the format of the items to be read. But I understand now, thanks to everyone!!

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by SimarGill View Post
    Ohh alright I got it. In my book, it just that says that scanf reads an input from stdin stream. The string pointed to by format specifies the format of the items to be read. But I understand now, thanks to everyone!!
    Seriously... get the library documentation specific to the compiler you are using... You will need to look these things up for yourself (which I do probably a hundred times a day) AND you will discover that your text book contains about 1/10th of the information you actually need.

  15. #15
    Registered User
    Join Date
    Nov 2011
    Location
    Canada
    Posts
    22
    Yes, I have downloaded it from my compilers website. And damn it, I spent $150 for that book.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ScanF Skipping The User Input.
    By XIIIX in forum C Programming
    Replies: 7
    Last Post: 10-11-2011, 07:00 AM
  2. Program skipping input....Scanf()
    By steve5591 in forum C Programming
    Replies: 2
    Last Post: 11-17-2010, 06:53 PM
  3. Compiler skipping a statement & moving to nxt instruction
    By MarjunC in forum C++ Programming
    Replies: 8
    Last Post: 06-29-2010, 05:34 AM
  4. Skipping if statement
    By FallenBlade in forum C Programming
    Replies: 6
    Last Post: 12-13-2009, 03:40 PM
  5. Skipping scanf()
    By Ican'tC in forum C Programming
    Replies: 4
    Last Post: 09-12-2002, 02:49 AM