Trouble with scanf

E

Eric A. Johnson

I am using scanf (from stdio.h) and having trouble inputting a date. I want
the user to be able to input a date in the format mm/dd/yyyy. I use the
following:
printf ("Please enter your first date in the form of mm/dd/yyyy: ");
scanf ("%i/%i/%i", &date1.month, &date1.day, &date1.year);
where date1 is a struct of type date, defined as:
struct date
{
int month;
int day;
int year;
};
Pretty simple, right? It works as expected when I input any date without
leading zeroes. However, when I input, for example, 09/07/1972, if it is
the first (of two) expected dates, it will fill the first date with zeroes,
and put the date into the second one (which is a copy of the previous two
lines for input, only for date2). If it is the second, it makes it all
zeroes and doesn't seem to register the second date. The same date,
however, works properly if I don't use any leading zeroes. Also, sometimes
it will work with one leading zero, or at least it seems to. Can anybody
enlighten me? I'm using a (junior) college textbook. Is there a different
C function I should use for input, instead? I'd like to use pure C for now,
and move on to learning C++ only once I'm rather skilled in C. Am I doing
something wrong, or is there simply something about scanf that can't handle
leading zeroes in integers?
 
M

Martin Ambuhl

Eric said:
scanf ("%i/%i/%i", &date1.month, &date1.day, &date1.year);
where date1 is a struct of type date, defined as:
struct date
{
int month;
int day;
int year;
};
Pretty simple, right? It works as expected when I input any date without
leading zeroes. However, when I input, for example, 09/07/1972, if it is
the first (of two) expected dates, it will fill the first date with zeroes,

If you don't want interpretation of the data with base determined as 8,
10, or 15 automatically, don't use the %i specifier. %i interprets
integral data as if you had used strtol with a base of 0. You want %d
which interprets integral data as if you had used strtol with a base of
10. All this is covered in any elementary C-for-dummies book. And you
might want to check the FAQ before posting.
 
E

Eric Sosman

Eric said:
I am using scanf (from stdio.h) and having trouble inputting a date. I want
the user to be able to input a date in the format mm/dd/yyyy. I use the
following:
printf ("Please enter your first date in the form of mm/dd/yyyy: ");
scanf ("%i/%i/%i", &date1.month, &date1.day, &date1.year);
where date1 is a struct of type date, defined as:
struct date
{
int month;
int day;
int year;
};
Pretty simple, right? It works as expected when I input any date without
leading zeroes. However, when I input, for example, 09/07/1972, if it is
the first (of two) expected dates, it will fill the first date with zeroes,
and put the date into the second one (which is a copy of the previous two
lines for input, only for date2). If it is the second, it makes it all
zeroes and doesn't seem to register the second date. The same date,
however, works properly if I don't use any leading zeroes. Also, sometimes
it will work with one leading zero, or at least it seems to. Can anybody
enlighten me? I'm using a (junior) college textbook. Is there a different
C function I should use for input, instead? I'd like to use pure C for now,
and move on to learning C++ only once I'm rather skilled in C. Am I doing
something wrong, or is there simply something about scanf that can't handle
leading zeroes in integers?

Study the difference between the "%i" conversion you
are using and the "%d" that probably suits your purpose
better. Note that a leading zero is merely a place-holder
for the latter, but has other significance for the former.

If that's not enough of a hint, try to compile

int i = 09;

.... and see what the compiler says about it. (Some compilers
may say nothing much unless you make sure to invoke them in a
Standard-conforming mode and with the warnings cranked up; if
you're using gcc I recommend "-ansi -pedantic -W -Wall" on the
command line.)
 
E

Eric A. Johnson

Thanks to both of you for your input. The book I'm using is "Programming in
ANSI C", Revised Edition, Copyright 1994 (although the book was used in a
class I took barely two years ago). It mentioned some of what you
mentioned, but failed to tell me all that you mentioned.
Addendum: I noticed it finally... 7 chapters after the exercise that asked
me to create the program that needs the dates inputted. This book should at
least have let me understand it earlier. Thanks again... and I'll check out
that FAQ.
 
C

CBFalconer

.... snip ...
Thanks to both of you for your input. The book I'm using is
"Programming in ANSI C", Revised Edition, Copyright 1994 (although
the book was used in a class I took barely two years ago). It
.... snip ...

Since you got helpful answers I expect you will be back. So you
should learn that topposting is not really acceptable in technical
newsgroups such as c.l.c. Your answer belongs after, or intermixed
with, the material to which you reply, with anything not germane to
your reply snipped.

Proper bottom-posting, as described above, is acceptable
everywhere.
 
E

Eric A. Johnson

... snip ...
Since you got helpful answers I expect you will be back. So you
should learn that topposting is not really acceptable in technical
newsgroups such as c.l.c. Your answer belongs after, or intermixed
with, the material to which you reply, with anything not germane to
your reply snipped.

Proper bottom-posting, as described above, is acceptable
everywhere.
.... snip ...

I had only posted in that fashion because I wished to thank both Martin
Ambuhl and Eric Sosman, yet I did not wish to make two replies that would,
in effect, say the same thing. Do you have any advice for when I wish to
thank numerous people for replying to my post without making multiple
replies... for example, if a dozen people helped me, each in their own
thread?
 
M

Mike Wahler

Eric A. Johnson said:
... snip ...

I had only posted in that fashion because I wished to thank both Martin
Ambuhl and Eric Sosman, yet I did not wish to make two replies that would,
in effect, say the same thing. Do you have any advice for when I wish to
thank numerous people for replying to my post without making multiple
replies... for example, if a dozen people helped me, each in their own
thread?

If more than one person answers, all the answers will be in
the *same* thread. I don't think you understand what a
Usenet thread is. It's the collection of all the messages
'attached to' and pertaining to (and including) the original
message (in this case, yours).

If you do see someone answer your question, but in some
other thread (not the one you asked in), they will be advised
to answer in the germane thread.

If you want to address several people, just post a single
message in the same thread, saying e.g. "Thanks Martin and Eric".
or "Thanks, everyone".

Finally, top-posting isn't an issue of *what* you write, but *how*.
You did correctly bottom-post this time, so I guess you understand
that part.

-Mike
 
M

Martin Ambuhl

Eric said:
Do you have any advice for when I wish to
thank numerous people for replying to my post without making multiple
replies... for example, if a dozen people helped me, each in their own
thread?

The best way to thank us is to stick around, asking good questions, and
eventually taking on part of the load by giving good answers to other
people's questions.
 
E

Eric A. Johnson

Martin Ambuhl said:
The best way to thank us is to stick around, asking good questions, and
eventually taking on part of the load by giving good answers to other
people's questions.

Will do! Thank you, all, for your help. I hope to learn a lot, and to
eventually become a contributor in my own right.
 

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

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,527
Members
44,999
Latest member
MakersCBDGummiesReview

Latest Threads

Top