Trouble with scanf

Discussion in 'C Programming' started by Eric A. Johnson, Jan 16, 2005.

  1. 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?
    Eric A. Johnson, Jan 16, 2005
    #1
    1. Advertising

  2. Eric A. Johnson wrote:
    > 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.
    Martin Ambuhl, Jan 16, 2005
    #2
    1. Advertising

  3. Eric A. Johnson

    Eric Sosman Guest

    Eric A. Johnson wrote:

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

    --
    Eric Sosman
    lid
    Eric Sosman, Jan 16, 2005
    #3
  4. 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.

    "Eric A. Johnson" <> wrote in message
    news:GVmGd.10920$...
    >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?
    >
    >
    Eric A. Johnson, Jan 16, 2005
    #4
  5. Eric A. Johnson

    CBFalconer Guest

    "Eric A. Johnson" wrote:
    > "Eric A. Johnson" <> wrote in message
    >
    >> 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:


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

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
    CBFalconer, Jan 16, 2005
    #5
  6. "CBFalconer" <> wrote in message
    news:...
    ... 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?
    Eric A. Johnson, Jan 16, 2005
    #6
  7. Eric A. Johnson

    Mike Wahler Guest

    "Eric A. Johnson" <> wrote in message
    news:QxyGd.2390$...
    >
    > "CBFalconer" <> wrote in message
    > news:...
    > > 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?


    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
    Mike Wahler, Jan 16, 2005
    #7
  8. Eric A. Johnson wrote:

    > 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.
    Martin Ambuhl, Jan 16, 2005
    #8
  9. "Martin Ambuhl" <> wrote in message
    news:...
    > Eric A. Johnson wrote:
    >
    >> 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.


    Will do! Thank you, all, for your help. I hope to learn a lot, and to
    eventually become a contributor in my own right.
    Eric A. Johnson, Jan 17, 2005
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hypo
    Replies:
    6
    Views:
    393
  2. Snubis

    Re: safe scanf( ) or gets

    Snubis, Jan 2, 2004, in forum: C++
    Replies:
    0
    Views:
    382
    Snubis
    Jan 2, 2004
  3. Peter Mount

    Trouble with scanf("%c", &answer);

    Peter Mount, Jun 14, 2004, in forum: C Programming
    Replies:
    14
    Views:
    681
    Zoran Cutura
    Jun 16, 2004
  4. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    scanf (yes/no) - doesn't work + deprecation errors scanf, fopen etc.

    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, Feb 16, 2006, in forum: C Programming
    Replies:
    185
    Views:
    3,339
    those who know me have no need of my name
    Apr 3, 2006
  5. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    difference between scanf("%i") and scanf("%d") ??? perhaps bug inVS2005?

    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, Apr 26, 2006, in forum: C Programming
    Replies:
    18
    Views:
    656
    Richard Bos
    May 2, 2006
Loading...

Share This Page