scanf to return after user presses enter?

This is a discussion on scanf to return after user presses enter? within the C Programming forums, part of the General Programming Boards category; Hi, I want the user to be able to enter up to 3 integers.... I need to react based on ...

  1. #1
    sp2
    sp2 is offline
    Registered User
    Join Date
    Apr 2008
    Posts
    7

    scanf to return after user presses enter?

    Hi,

    I want the user to be able to enter up to 3 integers.... I need to react based on the number they input, so i'm using scanf and tracking the return value. However, when I enter just one integer and press enter, it just moves to the next line and scanf is still running. Shouldn't scanf quit after the user presses enter?

    Example:
    Code:
    #include <stdio.h>
    
    int main()
    {
    	int one,two,three;
    	int ret_val = scanf("%d %d %d", &one, &two, &three);
    	if (ret_val == 0)
    	{
    	}
    	else if (ret_val == 1)
    	{
    		printf("one = %d\n",one);
    	}
    	else if (ret_val == 2)
    	{
    		printf("one = %d\ntwo = %d\n",one,two);
    	}
    	else
    	{
    		printf("one = %d\ntwo = %d\nthree = %d\n",one,two,three);
    	}
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by sp2 View Post
    Hi,

    I want the user to be able to enter up to 3 integers.... I need to react based on the number they input, so i'm using scanf and tracking the return value. However, when I enter just one integer and press enter, it just moves to the next line and scanf is still running. Shouldn't scanf quit after the user presses enter?
    No. scanf will stop when (a) the complete format string is matched; (b) input is given that doesn't match the format string; or (c) EOF happens. The enter key doesn't affect any of these.

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Example:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
    	int one,two,three, ret_val;
            char buffer[256];
    
            fgets(buffer, sizeof(buffer), stdin);
    	
            ret_val = sscanf(buffer, "&#37;d %d %d", &one, &two, &three);
            
            // Then make your cases from here using the knowledge tabstop imparted on you :)
    
            return 0;
    }

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by master5001 View Post
    Example:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
    	int one,two,three, ret_val;
            char buffer[256];
    
            fgets(buffer, sizeof(buffer), stdin);
    	
            ret_val = sscanf(buffer, "%d %d %d", &one, &two, &three);
            
            // Then make your cases from here using the knowledge tabstop imparted on you :)
    
            return 0;
    }
    I don't believe, strictly speaking, you need string.h to do this. Both man-pages and MSDN docs seem to agre with me.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,703
    I'm sure that scanf is line buffered though. So, in whole or in part, the format string, in addition to what tabstop said, should be consumed when you press enter.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by citizen View Post
    I'm sure that scanf is line buffered though. So, in whole or in part, the format string, in addition to what tabstop said, should be consumed when you press enter.
    Not quite. scanf() calls getchar() [or something very similar] to get the data from stdin. The stream of stdin itself is buffered, not scanf(), so if you enter 15 23<newline> to a scanf("%d", &x), then 15 will be consumed, the space, 23 and newline remain in the stdin buffer.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,703
    Hmm, I'll have to make a cheap implementation of scanf one of these days. Thanks for the correction.

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Well not only that, but my point wasn't to buffer scanf() it was to give you the opportunity to do an sscanf(buffer, "&#37;d %d %d", &a, &b, &c) != 3, sscanf(buffer, "%d %d", &a, &b) != 2, type of thing.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by master5001 View Post
    Well not only that, but my point wasn't to buffer scanf() it was to give you the opportunity to do an sscanf(buffer, "%d %d %d", &a, &b, &c) != 3, sscanf(buffer, "%d %d", &a, &b) != 2, type of thing.
    Yes, I'm with you on that. Read a string using fgets(), use the return value from sscanf() to get the number of actualy components filled in (0, 1, 2 or 3) - although the user could of course enter "1 a2 3", and you would think there was only one entry, when the user actually intented three - if that is necessary to cope with the original poster needs some more advanced logic.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Yeah man pages always have a habit of ganging up on me.... Meanwhile, at least this puts him off to a good start. Depending on his class level he may even impress his friends with his programs ability to cope with various user input.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  2. Newbie Help: Currency Converter
    By Ashfury in forum C Programming
    Replies: 10
    Last Post: 11-06-2005, 12:21 PM
  3. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM
  4. problem with open gl engine.
    By gell10 in forum Game Programming
    Replies: 1
    Last Post: 08-21-2003, 04:10 AM
  5. help with switch statements
    By Wexy in forum C Programming
    Replies: 3
    Last Post: 11-06-2002, 04:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21