scanf

This is a discussion on scanf within the C Programming forums, part of the General Programming Boards category; I am very new to c programming and dont mean to be wasting anyones time but after trying to use ...

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    2

    scanf

    I am very new to c programming and dont mean to be wasting anyones time but after trying to use the scanf function I am having some difficulties. I have created much more intracate programs (which isnt hard I know) but i havent used scanf, and I really cant see how this is going wrong.
    Code:
    int main(void){
    
    	int no, valid;
    	
    	printf("\nHow many would you like?\n\n           ");
    	
    	if(scanf("%d",&no) != 1){ // Checking to see only one argument has been entered
    	
    		printf("\nOnly one value.\n");
    		return (EXIT_FAILURE);
    		
    	}
    	
    	valid = scanf("%d",&no);
    	valid = ((valid != 0) && (valid != EOF));
    	
    	if( !valid ){ // Checking to see the correct data type has been entered
    	
    		printf("\nMust be an integer.\n");
    		return (EXIT_FAILURE);
    		
    	}	
    	
    	return (EXIT_SUCCESS);
    	
    }
    The error checks seem to not be working.

    when I type in 'a 2 ' it prints 'Only one value' but when I type '2 a' it prints 'Must be an integer'. But in both instances I am expecting 'Only one value' to be printed.

    Sorry for such a trivial problem but any help would be much appreciated

    Cheers
    Last edited by Salem; 12-10-2008 at 10:39 AM. Reason: colour adds nothing, if it's all the same colour

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    When you enter '2 a', the first scanf() only sucks in the 2 and leaves the a in the input buffer because scanf() is smart enough (dumb enough?) to stop parsing at the first whitespace it encounters. So the 'a' is left for your second scanf() to pull.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    2
    thank u for such a prompt reply, it has been very helpful

  4. #4
    Registered User haseth's Avatar
    Join Date
    Dec 2008
    Posts
    4
    You can use flushall(); after your first scanf so that it only reads the first scanf and removes the second one from the buffer.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,993
    Quote Originally Posted by haseth
    You can use flushall(); after your first scanf so that it only reads the first scanf and removes the second one from the buffer.
    Unfortunately flushall() does not appear to exist in the C standard library or cProgNoob's code.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf() consideres useless
    By Snafuist in forum C Programming
    Replies: 15
    Last Post: 02-18-2009, 07:35 AM
  2. Help with a basic scanf procedure.
    By killpoppop in forum C Programming
    Replies: 9
    Last Post: 11-03-2008, 03:39 PM
  3. Replies: 2
    Last Post: 02-20-2005, 12:48 PM
  4. Scanf and integer...
    By penny in forum C Programming
    Replies: 3
    Last Post: 04-24-2003, 06:36 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


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