[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

Scanf Problem

LJ

7/7/2011 4:20:00 AM

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??
5 Answers

John Gordon

7/7/2011 4:30:00 AM

0

In <eab46c07-89fa-43e1-a2ec-b44b69fcb80b@x41g2000yqd.googlegroups.com> LJ <djtheshowstopper@gmail.com> 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
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

LJ

7/7/2011 6:40:00 AM

0

On Jul 7, 9:29 am, John Gordon <gor...@panix.com> wrote:
> In <eab46c07-89fa-43e1-a2ec-b44b69fcb...@x41g2000yqd.googlegroups.com> LJ <djtheshowstop...@gmail.com> 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
> gor...@panix.com              B is for Basil, assaulted by bears
>                                 -- Edward Gorey, "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

Ike Naar

7/7/2011 7:28:00 AM

0

On 2011-07-07, LJ <djtheshowstopper@gmail.com> wrote:
> On Jul 7, 9:29?am, John Gordon <gor...@panix.com> wrote:
>> In <eab46c07-89fa-43e1-a2ec-b44b69fcb...@x41g2000yqd.googlegroups.com> LJ <djtheshowstop...@gmail.com> 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".

Malcolm McLean

7/7/2011 9:01:00 AM

0

On Jul 7, 9:40 am, LJ <djtheshowstop...@gmail.com> 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.site...

lawrence.jones

7/10/2011 7:32:00 PM

0

LJ <djtheshowstopper@gmail.com> 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