LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-24-2006, 10:55 AM   #1
ionuion
LQ Newbie
 
Registered: Dec 2004
Location: Moldova
Distribution: slackware
Posts: 5

Rep: Reputation: 0
scanf doesn't interrupt flow to read input


hello.
The idea behind my program is to get some numbers to send it asynchronously to a process. The decimals are red from the keyboard.

Code:
#include <stdio.h>

int main(void)
{
	printf("We need integer values. Could you provide us some, please?\n");
	printf("Press Ctrl-C when you have no more to give\n");
	while(1)
	{
		int temp;
		printf("Number? :");
		int ret = scanf("%d", &temp);
                // ferror associated to stdin is 0 at this point  
		if (!ret)
		{
			printf("This might not be a number.\n");
			printf("Please try again. What about right now?\n");
			continue;
		}
		else
		{
			// Good data. Can send them as useful
		}
	}
}
So, if the user sends an "it", the input shall be ignored, and user prompted for another number. At least that's what I expect. The fact is once an errouneous input is provided, the program behaves as the line containing the call to scanf didn't exist.
I tried to read ferror(stdin). It is 0 in any case.

Any hints will be endlessly appreciated.

Linux 2.6.14.2 PREEMPT i686 GNU/Linux
gcc (GCC) 3.3.6 (Debian 1:3.3.6-13)
 
Old 04-24-2006, 04:22 PM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,536

Rep: Reputation: 148Reputation: 148
That's how scanf() works. In your case, you should read a line as string (using getchar, fgets or read), then use atoi to convert it to a number and that, looking at the result, print the message or deal with the number you have.
 
Old 04-24-2006, 05:21 PM   #3
ioerror
Member
 
Registered: Sep 2005
Location: Old Blighty
Distribution: Slackware, NetBSD
Posts: 536

Rep: Reputation: 30
scanf is a function I try to steer clear of.

Quote:
then use atoi to convert it to a number
Better to use strtol, since atoi doesn't detect errors.
 
Old 04-25-2006, 07:45 AM   #4
ionuion
LQ Newbie
 
Registered: Dec 2004
Location: Moldova
Distribution: slackware
Posts: 5

Original Poster
Rep: Reputation: 0
Thank you for your help. I'll look with another pair of eyes at the scanf family.

Regards.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
verifing input from bash read michael_util Programming 1 01-13-2005 09:09 AM
read the input file and write in the given format suchi_s Programming 8 12-17-2004 01:12 AM
reading from STD IN (scanf , gets and read wont work) nodger Programming 1 09-01-2004 04:47 PM
Input/Output error during read on /dev/hdb yibble Fedora - Installation 4 05-25-2004 03:57 AM
input/output error during read on /dev/hda bm1 Linux - Hardware 36 11-28-2002 11:26 AM


All times are GMT -5. The time now is 09:33 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration