Thread: Scanf

  1. #1
    Registered User
    Join Date
    Aug 2019
    Posts
    138

    Scanf

    When using scanf, how do you check that only the Enter-key was pressed while an int was expected? This must be a very stupid question; sorry, I couldn't find the answer.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,066
    scanf is unlikely to be a good tool for the job here. I suggest:
    • Use fgets to read a line. If the resulting string only consists of "\n", then only the enter key was pressed.
    • Otherwise, use sscanf to parse the string for an int. (strtol is another option.)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    147
    The problem is that your scanf() call will not return until it encounters a string that is not whitespace only. You should use fgets() instead, as it prevents overflow as well.
    Code:
      char input[10];
      fgets(input, sizeof(input), stdin);
      if( input[0] == '\n' ) {
        puts("line is empty");
      };
    reference:
    string - How to get empty input or only ENTER in C - Stack Overflow


  4. #4
    Registered User
    Join Date
    Aug 2019
    Posts
    138
    Quote Originally Posted by laserlight View Post
    scanf is unlikely to be a good tool for the job here. I suggest:
    • Use fgets to read a line. If the resulting string only consists of "\n", then only the enter key was pressed.
    • Otherwise, use sscanf to parse the string for an int. (strtol is another option.)
    The code using the int that is being input is a switch statement. I will have to do some trials with your suggestion for which many thanks.

  5. #5
    Registered User
    Join Date
    Aug 2019
    Posts
    138
    Quote Originally Posted by laserlight View Post
    scanf is unlikely to be a good tool for the job here. I suggest:
    • Use fgets to read a line. If the resulting string only consists of "\n", then only the enter key was pressed.
    • Otherwise, use sscanf to parse the string for an int. (strtol is another option.)
    The solution turned out to be dead simple: - the problem was in menu() - so: replace "scanf()" by "get char", use "case '1' etc in "switch", and use "menu()" as the default option. Thank you again for helping.

  6. #6
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    659
    Quote Originally Posted by zach View Post
    The solution turned out to be dead simple: - the problem was in menu() - so: replace "scanf()" by "get char", use "case '1' etc in "switch", and use "menu()" as the default option. Thank you again for helping.
    The answer IS dead simple, and provided by @laserlight.

    Don't use scanf() or getchar(). Use fgets() to get the whole string in, including the newline, and check the first char in the char array, if it is a newline.

    scanf() can leave the newline in the input buffer, and getchar() does, if the user enters any chars before pressing the Enter key. Further data entry in the program is complicated by the newline left in the input buffer.

    C is line buffered, and all character data entry is best handled by fgets(), The input string can then be easily parsed as needed in memory.

    Please keep in mind that we speak from experience!

  7. #7
    Registered User
    Join Date
    Aug 2019
    Posts
    138
    [QUOTE=rstanley;1289634]The answer IS dead simple, and provided by @laserlight.

    Don't use scanf() or getchar(). Use fgets() to get the whole string in, including the newline, and check the first char in the char array, if it is a newline.
    ~~~~~~~~~~~~~~~~~~~~~~
    The issue is being complicated because of needing to input an integer and not a string.

  8. #8
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    659
    [QUOTE=zach;1289637]
    Quote Originally Posted by rstanley View Post
    The answer IS dead simple, and provided by @laserlight.

    Don't use scanf() or getchar(). Use fgets() to get the whole string in, including the newline, and check the first char in the char array, if it is a newline.
    ~~~~~~~~~~~~~~~~~~~~~~
    The issue is being complicated because of needing to input an integer and not a string.
    Using fgets(), all input from the keyboard in text. You get the string in with fgets(), and convert the string to an integer with atol(), or strtol(), confirm the value converted is within the valid range of an int, and assign to the int variable.

    You could convert directly to an int, but I recommend the extra work to prevent overflow of an int, especially with atol(), which does no error checking.

    This is standard for real life applications.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,066
    The issue is being complicated because of needing to input an integer and not a string.
    The user inputs an integer (or something else) as text. You then parse this into an int (or other integer type). So, it isn't complicated: you merely separate reading from parsing for better control.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Aug 2019
    Posts
    138
    Quote Originally Posted by laserlight View Post
    scanf is unlikely to be a good tool for the job here. I suggest:
    • Use fgets to read a line. If the resulting string only consists of "\n", then only the enter key was pressed.
    • Otherwise, use sscanf to parse the string for an int. (strtol is another option.)
    I already responded but have another question. In a number of instances in the program I use a switch statement. In those instances there are no more problems. However one situation remains. There I tried the "strtol" option you suggested. The strtol-statement generates a number okay, but the string part being output by the strtol-statement is an empty string.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,066
    Quote Originally Posted by zach View Post
    I already responded but have another question. In a number of instances in the program I use a switch statement. In those instances there are no more problems. However one situation remains. There I tried the "strtol" option you suggested. The strtol-statement generates a number okay, but the string part being output by the strtol-statement is an empty string.
    That means there are no extra characters after the integer that was parsed.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Aug 2019
    Posts
    138
    Quote Originally Posted by laserlight View Post
    The user inputs an integer (or something else) as text. You then parse this into an int (or other integer type). So, it isn't complicated: you merely separate reading from parsing for better control.
    Okay, I will give it more tries. Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf help?
    By Teardrop3903 in forum C Programming
    Replies: 25
    Last Post: 02-17-2011, 06:27 PM
  2. First scanf() skips next scanf() !
    By grahampatten in forum C Programming
    Replies: 5
    Last Post: 08-17-2004, 02:47 AM
  3. scanf() <-> esc key
    By ipe in forum C Programming
    Replies: 5
    Last Post: 03-12-2003, 10:27 AM
  4. scanf
    By beginner2003 in forum C Programming
    Replies: 5
    Last Post: 02-09-2003, 08:55 AM
  5. scanf - data is "put back" - screws up next scanf
    By voltson in forum C Programming
    Replies: 10
    Last Post: 10-14-2002, 04:34 AM

Tags for this Thread