Thread: scanf

  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 11: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
    Chappell Hill, Texas
    Posts
    2,332
    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.
    Mainframe assembler programmer by trade. C coder when I can.

  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
    28,413
    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.
    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

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, 08:35 AM
  2. Help with a basic scanf procedure.
    By killpoppop in forum C Programming
    Replies: 9
    Last Post: 11-03-2008, 04:39 PM
  3. Replies: 2
    Last Post: 02-20-2005, 01: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