C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 12-10-2008, 07:01 AM   #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
cProgNoob is offline   Reply With Quote
Old 12-10-2008, 07:14 AM   #2
Jack of many languages
 
Join Date: Nov 2007
Location: Katy, Texas
Posts: 1,929
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.

Memorable Quotes From Recent Posts:

I can't remember.
Dino is offline   Reply With Quote
Old 12-10-2008, 08:16 AM   #3
Registered User
 
Join Date: Dec 2008
Posts: 2
thank u for such a prompt reply, it has been very helpful
cProgNoob is offline   Reply With Quote
Old 12-10-2008, 10:58 AM   #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.
haseth is offline   Reply With Quote
Old 12-10-2008, 11:04 AM   #5
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,352
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
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Reply

Tags
error, scanf

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
scanf() consideres useless Snafuist C Programming 15 02-18-2009 08:35 AM
Help with a basic scanf procedure. killpoppop C Programming 9 11-03-2008 04:39 PM
Odd data entry error, reading an unsigned integer with scanf Xipher C Programming 2 02-20-2005 01:48 PM
Scanf and integer... penny C Programming 3 04-24-2003 06:36 AM
scanf - data is "put back" - screws up next scanf voltson C Programming 10 10-14-2002 04:34 AM


All times are GMT -6. The time now is 10:57 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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