Issue with scanf and char

This is a discussion on Issue with scanf and char within the C Programming forums, part of the General Programming Boards category; Hi, So I get this simple thing here, meant to get a number between 1 and 12. The problem is, ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    16

    Issue with scanf and char

    Hi,

    So I get this simple thing here, meant to get a number between 1 and 12. The problem is, in my program, if I enter a character, I get an infinite loop and I just can't find a way to fix it. Any idea?

    Code:
    int demanderposition1()
    {
    	int position1=1;
    	
    	do
    	{	
    		if(position1<=0 || position1>12)
    		{
    			printf("Erreur de saisie, veuillez recommencer.\n");
    		}
    		printf("Faites votre 1er choix de position entre 1 et 12 :");
    		fflush(stdin);
    		scanf("%d", &position1);
    	}while((position1<=0 || position1>12));
    
    
    	return position1;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you don't know that the input that comes in is going to agree with the pattern (in this case, if they are using a keyboard that has anything other than 0-9 on it), then scanf is not an appropriate choice. To get a line of input for further processing, use fgets with a character buffer. You can then attempt to read with sscanf, or if they typed in garbage, throw it away and try again.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    16
    And what if I know it's a keyboard with numbers from 0 to 9? :P

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you know that they're using a keyboard with only the 0123456789, then you don't have to worry about them pressing 'b'.

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    16
    Well sorry, I meant a full keyboard that included too 0 to 9 ^^'

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Then scanf is not an appropriate choice. Use fgets to read into a character buffer, and check the input to see if it's a number (using sscanf or strtol).

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,344
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf to read int but read in char?
    By maybabier in forum C Programming
    Replies: 3
    Last Post: 04-11-2009, 12:58 AM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. the 50 years old mom is back and need help again
    By laraine andaya in forum C Programming
    Replies: 8
    Last Post: 08-11-2002, 03:00 PM
  4. File Input / Output Help?
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 05-18-2002, 10:20 AM
  5. Scanf for char doesn't work in loop
    By Unregistered in forum C Programming
    Replies: 10
    Last Post: 10-18-2001, 04:42 AM

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