473,394 Members | 1,893 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

infinite loop with scanf

Hi,

why does the following result in an infinite loop when the user does
*not* enter a number?

do{
printf("Enter Number: ");
} while ( ! scanf("%i", &input) );
Thanks,
Michael Goerz

Oct 17 '06 #1
4 6120
On Tue, 17 Oct 2006 22:14:06 +0200, Michael Goerz
<ne****@8439.e4ward.comwrote in comp.lang.c:
Hi,

why does the following result in an infinite loop when the user does
*not* enter a number?

do{
printf("Enter Number: ");
} while ( ! scanf("%i", &input) );
Thanks,
Michael Goerz
This is a very frequently asked question by beginning C programmers,
and as such it is in the FAQ for this group.

See http://c-faq.com/stdio/scanfjam.html

And you will very probably find a lot of useful information in the
rest of the FAQ as well. The link to its main page is in my signature
block.

Hint, always check to see if a newsgroup has a FAQ, and check for an
answer to your question if it does, before posting a question to a
group.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Oct 17 '06 #2

"Michael Goerz" <ne****@8439.e4ward.comwrote in message
news:4p************@uni-berlin.de...
Hi,

why does the following result in an infinite loop when the user does
*not* enter a number?

do{
printf("Enter Number: ");
} while ( ! scanf("%i", &input) );
Thanks,
Michael Goerz
Think about where the file pointer will be after the scanf call
when a non-integer is entered.

--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project
Oct 17 '06 #3
Michael Goerz wrote:
>
why does the following result in an infinite loop when the user
does *not* enter a number?

do{
printf("Enter Number: ");
} while ( ! scanf("%i", &input) );
Because nothing flushes the input data that does not form a
number. If something beginning with 'z', for example, does not
form a number, it still doesn't form a number on rescan.

Don't use scanf for interactive input. It bites.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Oct 18 '06 #4
Fred Kleinschmidt said:

<snip>
Think about where the file pointer will be after the scanf call
when a non-integer is entered.
Right where it was, of course, but I think you mean the file position
indicator.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Oct 18 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

43
by: Gremlin | last post by:
If you are not familiar with the halting problem, I will not go into it in detail but it states that it is impossible to write a program that can tell if a loop is infinite or not. This is a...
5
by: mailpitches | last post by:
Hello, Is there any way to kill a Javascript infinite loop in Safari without force-quitting the browser? MP
7
by: Rob | last post by:
I'm stuck in a program I'm making. I'm supposed to have a menu that asks the user to choose an option between 1 and 3. If the user chooses a valid option, i.e. types a number from 1 to 3, then...
13
by: Redduck | last post by:
Hello everyone. I am frustrated, I have written the simple program below for a class and I am having problems with the DO-WHILE loop. On the first run through the loop, everything works well, the...
7
by: vjay | last post by:
I want to just create a linklist.The program below goes into an endless loop.The srange behaviour is that i can exit from the program if i create only two nodes.After two goes into infinite loop. ...
2
by: ija | last post by:
i trace the code, why compiler didn't trace to capture %c... why... why.. help me someone pls.. -------- void main () { char a, morechar; do { clrscr(); printf("input a char :");...
33
by: dmoran21 | last post by:
Hi all, I am a mathematician and I'm trying to write a program to try out a formula that I've derived. However, it seems that I've got an infinite loop and I don't quite understand why. I was...
44
by: James Watt | last post by:
can anyone tell me how to do an infinite loop in C/C++, please ? this is not a homework question .
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.