Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Scanf Problem

Reply
Thread Tools

Scanf Problem

 
 
LJ
Guest
Posts: n/a
 
      07-07-2011
K&R says that the scanf format string may contain ordinary characters,
which are expected to match the next non-white space character of the
input stream.

However when I am reading the date given as 25 /
12 / 1988 using scanf( "%d/%d/%d", &day, &month,
&year ); it is only reading the day. Why is it not reading the month
and year??
 
Reply With Quote
 
 
 
 
John Gordon
Guest
Posts: n/a
 
      07-07-2011
In <(E-Mail Removed)> LJ <(E-Mail Removed)> writes:

> K&R says that the scanf format string may contain ordinary characters,
> which are expected to match the next non-white space character of the
> input stream.


> However when I am reading the date given as 25 /
> 12 / 1988 using scanf( "%d/%d/%d", &day, &month,
> &year ); it is only reading the day. Why is it not reading the month
> and year??


Probably because your input contains spaces which the format string wasn't
told to look for.

--
John Gordon A is for Amy, who fell down the stairs
http://www.velocityreviews.com/forums/(E-Mail Removed) B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

 
Reply With Quote
 
 
 
 
LJ
Guest
Posts: n/a
 
      07-07-2011
On Jul 7, 9:29*am, John Gordon <(E-Mail Removed)> wrote:
> In <(E-Mail Removed)> LJ<(E-Mail Removed)> writes:
>
> > K&R says that the scanf format string may contain ordinary characters,
> > which are expected to match the next non-white space character of the
> > input stream.
> > However when I am reading the date given as 25 * * * * * /
> > 12 * * */ * * * * 1988 *using scanf( "%d/%d/%d", &day, &month,
> > &year ); it is only reading the day. Why is it not reading the month
> > and year??

>
> Probably because your input contains spaces which the format string wasn't
> told to look for.
>
> --
> John Gordon * * * * * * * * * A is for Amy, who fell down the stairs
> (E-Mail Removed) * * * * * * *B is for Basil, assaulted by bears
> * * * * * * * * * * * * * * * * -- EdwardGorey, "The Gashlycrumb Tinies"

That doesn't answer my question, '/' should match the next non-
whitespace character and in input that character is '/' only, so scanf
shud read all the three dates
 
Reply With Quote
 
Ike Naar
Guest
Posts: n/a
 
      07-07-2011
On 2011-07-07, LJ <(E-Mail Removed)> wrote:
> On Jul 7, 9:29?am, John Gordon <(E-Mail Removed)> wrote:
>> In <(E-Mail Removed)> LJ <(E-Mail Removed)> writes:
>>
>> > K&R says that the scanf format string may contain ordinary characters,
>> > which are expected to match the next non-white space character of the
>> > input stream.
>> > However when I am reading the date given as 25 ? ? ? ? ? /
>> > 12 ? ? ?/ ? ? ? ? 1988 ?using scanf( "%d/%d/%d", &day, &month,
>> > &year ); it is only reading the day. Why is it not reading the month
>> > and year??

>>
>> Probably because your input contains spaces which the format string wasn't
>> told to look for.

>
> That doesn't answer my question, '/' should match the next non-
> whitespace character and in input that character is '/' only, so scanf
> shud read all the three dates


It does answer your question; you should tell scanf to skip
the whitespace.
Read your scanf documentation. Hint: "d% / %d / %d".
 
Reply With Quote
 
Malcolm McLean
Guest
Posts: n/a
 
      07-07-2011
On Jul 7, 9:40*am, LJ <(E-Mail Removed)> wrote:
>
> That doesn't answer my question, '/' should match the next non-
> whitespace character and in input that character is '/' only, so scanf
> shud read all the three dates
>

The documentation you read is a bit confusing.

Whitespace matches whitespace, all other characters match themselves,
except for format fields introduced by %, and the % character itself.
--
Lots of programming goodies on my website
http://www.malcolmmclean.site11.com/www

 
Reply With Quote
 
lawrence.jones@siemens.com
Guest
Posts: n/a
 
      07-10-2011
LJ <(E-Mail Removed)> wrote:
> K&R says that the scanf format string may contain ordinary characters,
> which are expected to match the next non-white space character of the
> input stream.
>
> However when I am reading the date given as 25 /
> 12 / 1988 using scanf( "%d/%d/%d", &day, &month,
> &year ); it is only reading the day. Why is it not reading the month
> and year??


Because the paraphrase above is not correct: ordinary characters are
expected to match the next characters from the stream, they do not skip
leading whitespace.
--
Larry Jones

It COULD'VE happened by accident! -- Calvin
 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
difference between scanf("%i") and scanf("%d") ??? perhaps bug inVS2005? =?ISO-8859-1?Q?Martin_J=F8rgensen?= C Programming 18 05-02-2006 10:53 AM
scanf (yes/no) - doesn't work + deprecation errors scanf, fopen etc. =?ISO-8859-1?Q?Martin_J=F8rgensen?= C Programming 185 04-03-2006 02:49 PM
scanf/getchar sequence problem clusardi2k@aol.com C++ 21 04-20-2005 09:50 AM
Re: printf scanf problem Dan Pop C Programming 0 08-04-2003 01:54 PM
Re: printf scanf problem code_wrong C Programming 1 08-02-2003 08:10 PM



Advertisments