Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | jt@toerring.de (Jens Thoms Toerring) |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: A finalized program |
| Date | 2015-09-10 22:37 +0000 |
| Organization | Freie Universitaet Berlin |
| Message-ID | <d5ef25F1jopU1@mid.uni-berlin.de> (permalink) |
| References | <d93ca264-e41a-4f82-8c09-cd87731b2eed@googlegroups.com> <d586scFdva0U1@mid.uni-berlin.de> <2e6deec0-c080-41f5-8f1c-18d27e46621f@googlegroups.com> |
Alla _ <modelling.data@gmail.com> wrote:
> On Tuesday, September 8, 2015 at 5:41:49 PM UTC+4, Jens Thoms Toerring wrote:
> I am trying to understand this part.
> Please, correct me if I am wrong in my understanding
> char card_number[ 17 ];
> > printf( "Enter card number: " );
> > while ( ! ( scanf( "%16[0-9]", card_number ) == 1
> > && getchar( ) == '\n' ) )
> /* while the user's input is not a numerical number
> (while (!(scanf( "%16[0-9]", card_number ) == 1..)
> and while there are still some characters in the user's
> input (...&& getchar() == '\n')
> tell the user to re-enter the card number. Is it a correct
> interpretation of this part? */
> > {
> > printf( "Not a card number, please re-ennter it: " );
> > while ( getchar( ) != '\n' )
> /* I don't understand the function and meaning of
> the while ( getchar( ) != '\n' ) used inside the loop.
> What else does this add to the same line within while
> loop condition? Isn't while ( getchar( ) != '\n' )
> equal to while ( ! ( getchar( ) == '\n' ) )?
Assume that scanf() stopped reading because it detected
a character in the input that wasn't a digit. Now, if you
call scanf() again, what will it "see" as the first thing?
Let's start with the question where scanf() is getting all
those characters from. The underlying system has a largish
bugger for temporarily storing what the user types in. And
it only passes them over to scanf() once the user hit the
<ENTER> key (which gets translated into a '\n' character).
Once scanf() can get at them it reads them in (typically
one by one) and checks if they fit the format specifier,
accepting them if possible. The ones that don't fit make
scanf() stop and stay in that input buffer for later re-
trieval.
Thus, if scanf() had to reject something (in your case
because it wasn't the expected digit) it stays in the
input buffer scanf() reads from. And when you call scanf()
again, asking again for digits only, the first thing scanf()
will see is the non-digit character which made it stop al-
ready the last time round - and making it stop again! That
way it's very easy to create an infinite loop.
So what's the loop
while ( getchar( ) != '\n' )
/* empty */ ;
is good for? It keeps reading characters from the same input
buffer scanf() is reading from, throwing them all away until
after an end-of-line character was found. Thus effectively
all the bad input the user entered (which would trip scanf()
on the next call) is discarded. And thus on the next call
scanf() will see only the next line of text the user typed
- which hopefully is more suitable.
To make it more visual asssume the user entered
5237a2932\n
With a "%16[0-9]" format specifier scanf will stop here
5237a2932\n
^
since 'a' isn't a digit. And that 'a', together with all of
the rest remains in the input buffer, which now contains
a2932\n
Obviously, calling scanf() with "%16[0-9]" again won't do
since it will now reject already the very first character
(the 'a'). But by using that while loop you'll get to be-
hind the end-of-line character
a2932\n
^
so the input buffer is now cleared of everything of that
bad line. And now a call of scanf() will get the next line
of text the user types in (this time hopefully without any
more mistakes).
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-08 04:13 -0700
Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-08 04:19 -0700
Re: A finalized program fir <profesor.fir@gmail.com> - 2015-09-08 04:30 -0700
Re: A finalized program Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-09-08 14:45 +0100
Re: A finalized program Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-09-08 08:29 -0700
Re: A finalized program Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-09-08 08:52 -0700
Re: A finalized program Martin Shobe <martin.shobe@yahoo.com> - 2015-09-08 13:01 -0500
Re: A finalized program Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-09-08 21:00 +0100
Re: A finalized program Malcolm McLean <malcolm.mclean5@btinternet.com> - 2015-09-08 13:33 -0700
Re: A finalized program Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-09-09 00:28 +0100
Re: A finalized program Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-09-08 14:26 +0100
Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-08 06:42 -0700
Re: A finalized program Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-09-08 20:50 +0100
Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-11 01:47 -0700
Re: A finalized program jt@toerring.de (Jens Thoms Toerring) - 2015-09-08 13:41 +0000
Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-10 01:31 -0700
Re: A finalized program jt@toerring.de (Jens Thoms Toerring) - 2015-09-10 10:39 +0000
Re: A finalized program David Thompson <dave.thompson2@verizon.net> - 2015-09-20 18:35 -0400
Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-10 01:33 -0700
Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-10 10:16 -0700
Re: A finalized program bartekltg <bartekltg@gmail.com> - 2015-09-10 20:08 +0200
Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-11 02:05 -0700
Re: A finalized program Richard Heathfield <rjh@cpax.org.uk> - 2015-09-11 10:17 +0100
Re: A finalized program Richard Heathfield <rjh@cpax.org.uk> - 2015-09-11 10:19 +0100
Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-11 02:28 -0700
Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-11 02:29 -0700
Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-11 02:19 -0700
Re: A finalized program Richard Heathfield <rjh@cpax.org.uk> - 2015-09-11 10:23 +0100
Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-11 02:33 -0700
Re: A finalized program Keith Thompson <kst-u@mib.org> - 2015-09-11 14:11 -0700
Re: A finalized program jt@toerring.de (Jens Thoms Toerring) - 2015-09-11 21:39 +0000
Re: A finalized program bartekltg <bartekltg@gmail.com> - 2015-09-11 12:49 +0200
Re: A finalized program Richard Heathfield <rjh@cpax.org.uk> - 2015-09-10 23:31 +0100
Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-11 02:15 -0700
Re: A finalized program jt@toerring.de (Jens Thoms Toerring) - 2015-09-10 22:37 +0000
Re: A finalized program Richard Heathfield <rjh@cpax.org.uk> - 2015-09-10 23:47 +0100
Re: A finalized program jt@toerring.de (Jens Thoms Toerring) - 2015-09-11 10:18 +0000
Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-11 01:07 -0700
Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-11 02:14 -0700
Re: A finalized program jt@toerring.de (Jens Thoms Toerring) - 2015-09-11 10:10 +0000
Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-11 04:19 -0700
Re: A finalized program Barry Schwarz <schwarzb@dqel.com> - 2015-09-08 15:53 -0700
Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-11 04:27 -0700
Re: A finalized program bartekltg <bartekltg@gmail.com> - 2015-09-11 14:05 +0200
Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-11 04:42 -0700
Re: A finalized program bartekltg <bartekltg@gmail.com> - 2015-09-11 14:15 +0200
Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-11 05:50 -0700
Re: A finalized program bartekltg <bartekltg@gmail.com> - 2015-09-11 15:00 +0200
Re: A finalized program Alla _ <modelling.data@gmail.com> - 2015-09-11 05:50 -0700
Re: A finalized program Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-09-11 13:46 +0100
Re: A finalized program Richard Heathfield <rjh@cpax.org.uk> - 2015-09-11 14:19 +0100
csiph-web