Scanf Problem

L

LJ

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??
 
J

John Gordon

In said:
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.
 
L

LJ

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
 
I

Ike Naar

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".
 
M

Malcolm McLean

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.
 
L

lawrence.jones

LJ said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Problem with scanf 7
Taskcproblem calendar 4
scanf and loop 6
Help with code plsss 0
fflush(stdin), scanf and a space 5
advanced scanf 2
fflush(stdin), scanf and a space 5
Collect Excel Data from Website 5

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top