Skipped input

Discussion in 'C Programming' started by Sandy Beech, Nov 25, 2010.

  1. Sandy Beech

    Sandy Beech Guest

    Hi

    I am doing an exercise involving time in C, the task is to check a users
    age against his date-of-birth. However after displaying the prompt for
    date-of-birth, the program skips immediately to the output without
    leaving any chance for the user to input a date of birth.

    I have tried putting fflush(stdin) before the prompt, but that didnt work
    either, can anyone help.

    #include<stdio.h>
    #include<time.h>

    void main()
    {
    char name[80];
    int age;
    char dob[80];
    struct tm dob_tm;
    time_t now=time(0);
    printf("what is you're name:\n");
    gets(name);
    printf("what is you're age:\n");
    scanf("%d", &age);
    printf("enter you're date of birth (mm/dd/yy):\n");
    fflush(stdin);
    gets(dob);
    strptime(dob,"%D",&dob_tm);
    printf("hello %s,",name);
    if(localtime(&now)->tm_year-dob_tm.tm_year!=age)
    printf("you lied about you're age, you are %d!!\n"
    ,localtime(&now)->tm_year-dob_tm.tm_year);
    }
    Sandy Beech, Nov 25, 2010
    #1
    1. Advertising

  2. Sandy Beech

    Stefan Ram Guest

    Sandy Beech <> writes:
    >"you lied about you're age, you are %d!!


    That should be:

    »You lied about your age, your date of birth, or both,
    or this program has made a mistake, or the program is
    correct and you did not lie, but a high-energy particle
    from the cosmic radiation flipped a bit, or the
    processor has had a bug, or, well whatever.«
    Stefan Ram, Nov 25, 2010
    #2
    1. Advertising

  3. On Thu, 25 Nov 2010 21:39:19 +0000 (UTC), Sandy Beech
    <> wrote:

    >Hi
    >
    >I am doing an exercise involving time in C, the task is to check a users
    >age against his date-of-birth. However after displaying the prompt for
    >date-of-birth, the program skips immediately to the output without
    >leaving any chance for the user to input a date of birth.
    >
    >I have tried putting fflush(stdin) before the prompt, but that didnt work


    Since fflush is defined only for output streams, this invoked
    undefined behavior.

    >either, can anyone help.
    >
    >#include<stdio.h>
    >#include<time.h>
    >
    >void main()
    >{
    > char name[80];
    > int age;
    > char dob[80];
    > struct tm dob_tm;
    > time_t now=time(0);
    > printf("what is you're name:\n");
    > gets(name);


    Ignoring the fact that gets can lead to buffer overruns, think very
    carefully about every key you pressed to enter this information.
    Assume that every key generates one character in the input stream. How
    many characters did you generate? How many of those characters were
    stored in name[]? What caused gets() to stop transferring characters
    to name[]? What happened with the remaining characters?

    > printf("what is you're age:\n");
    > scanf("%d", &age);


    Again, be very precise with the keys. As scanf() processed the
    characters in the stream, what was the first character it saw? What
    was the first significant character? What caused it to stop scanning?
    What happened with the remaining characters?

    > printf("enter you're date of birth (mm/dd/yy):\n");
    > fflush(stdin);
    > gets(dob);


    What did gets() find in the stream that led it to believe it was done?

    > strptime(dob,"%D",&dob_tm);


    This is not a standard C function.

    > printf("hello %s,",name);
    > if(localtime(&now)->tm_year-dob_tm.tm_year!=age)


    Unrelated to your C question but your test is a little simplistic.

    A person born 1 Jan 1971 would probably answer 39 to your request for
    age, hoping to avoid the big 4-0 as long as possible. A kid born 1
    Jan 2000 would probably answer 11, hoping to grow up as fast as
    possible and well over 10.5.

    Furthermore, a person born 1 Jan 1946 can claim to be 65 on 31 Dec
    2010 for US tax purposes.

    > printf("you lied about you're age, you are %d!!\n"
    > ,localtime(&now)->tm_year-dob_tm.tm_year);


    You have computed this expression previously. It would be better to
    save the result once and reuse it as needed (unless you plan to run
    the program over midnight 31 Dec - 1 Jan).

    >}


    Once you get the program working, think about replacing gets() with
    fgets() and making it your standard habit.

    --
    Remove del for email
    Barry Schwarz, Nov 25, 2010
    #3
  4. Sandy Beech

    Seebs Guest

    On 2010-11-25, Sandy Beech <> wrote:
    > I am doing an exercise involving time in C, the task is to check a users
    > age against his date-of-birth. However after displaying the prompt for
    > date-of-birth, the program skips immediately to the output without
    > leaving any chance for the user to input a date of birth.
    >
    > I have tried putting fflush(stdin) before the prompt, but that didnt work
    > either, can anyone help.


    With the number of @nospam.com people posting blatant trolls via aioe.org
    these days, and the number of red flags in your post, I'd guess the answer
    is that only a competent therapist can help you.

    -s
    --
    Copyright 2010, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    I am not speaking for my employer, although they do rent some of my opinions.
    Seebs, Nov 26, 2010
    #4
  5. Sandy Beech

    Jim Dude Guest

    >   printf("what is you're name:\n");
    >   printf("enter you're date of birth (mm/dd/yy):\n");


    There are important syntax errors in both lines.

    Jimbo
    (Helpfull as ever)
    Jim Dude, Nov 26, 2010
    #5
  6. On Fri, 26 Nov 2010 00:11:30 -0800 (PST), Jim Dude
    <> wrote:

    >>   printf("what is you're name:\n");
    >>   printf("enter you're date of birth (mm/dd/yy):\n");

    >
    >There are important syntax errors in both lines.


    Really? Would you care to elaborate?

    --
    Remove del for email
    Barry Schwarz, Nov 26, 2010
    #6
  7. Sandy Beech

    Stefan Ram Guest

    Barry Schwarz <> writes:
    >On Fri, 26 Nov 2010 00:11:30 -0800 (PST), Jim Dude
    ><> wrote:
    >>>printf("what is you're name:\n");
    >>>printf("enter you're date of birth (mm/dd/yy):\n");

    >>There are important syntax errors in both lines.

    >Really? Would you care to elaborate?


    It seems as if that should be:

    printf( "What is your name?\n" );
    printf( "Enter your date of birth (mm/dd/yy):\n" );

    .
    Stefan Ram, Nov 26, 2010
    #7
  8. In article <-berlin.de>,
    Stefan Ram <-berlin.de> wrote:
    >Barry Schwarz <> writes:
    >>On Fri, 26 Nov 2010 00:11:30 -0800 (PST), Jim Dude
    >><> wrote:
    >>>>printf("what is you're name:\n");
    >>>>printf("enter you're date of birth (mm/dd/yy):\n");
    >>>There are important syntax errors in both lines.

    >>Really? Would you care to elaborate?

    >
    > It seems as if that should be:
    >
    >printf( "What is your name?\n" );
    >printf( "Enter your date of birth (mm/dd/yy):\n" );
    >
    > .
    >


    Or, more simply:

    puts("What is your name?");
    puts("Enter your date of birth (mm/dd/yy):");

    --
    Religion is regarded by the common people as true,
    by the wise as foolish,
    and by the rulers as useful.

    (Seneca the Younger, 65 AD)
    Kenny McCormack, Nov 26, 2010
    #8
  9. Sandy Beech

    Stefan Ram Guest

    Vincenzo Mercuri <> writes:
    >Eheh...not a big deal from the C language point of view...


    »Besides a mathematical inclination, an exceptionally
    good mastery of one's native tongue is the most vital
    asset of a competent programmer.«

    attributed to Djikstra, but I can not confirm the source

    »If your writing is semi-literate, ungrammatical, and
    riddled with misspellings, many hackers (including myself)
    will tend to ignore you. While sloppy writing does not
    invariably mean sloppy thinking, we've generally found the
    correlation to be strong -- and we have no use for sloppy
    thinkers. If you can't yet write competently, learn to.«

    Eric Raymond

    http://www.catb.org/~esr/faqs/hacker-howto.html#skills4

    »I've found that some of the best developers of all are
    English majors. They'll often graduate with no programming
    experience at all, and certainly without a clue about the
    difference between DRAM and EPROM.

    But they can write. That's the art of conveying
    information concisely and clearly. Software development
    and writing are both the art of knowing what you're going
    to do, and then lucidly expressing your ideas.«

    http://praisecurseandrecurse.blogspot.com/2007/03/english-majors-as-programmers.html
    Stefan Ram, Nov 26, 2010
    #9
  10. Stefan Ram wrote:
    > Vincenzo Mercuri<> writes:
    >> Eheh...not a big deal from the C language point of view...

    >
    > »Besides a mathematical inclination, an exceptionally
    > good mastery of one's native tongue is the most vital
    > asset of a competent programmer.«
    >
    > attributed to Djikstra, but I can not confirm the source
    >
    > »If your writing is semi-literate, ungrammatical, and
    > riddled with misspellings, many hackers (including myself)
    > will tend to ignore you. While sloppy writing does not
    > invariably mean sloppy thinking, we've generally found the
    > correlation to be strong -- and we have no use for sloppy
    > thinkers. If you can't yet write competently, learn to.«
    >
    > Eric Raymond
    >
    > http://www.catb.org/~esr/faqs/hacker-howto.html#skills4
    >
    > »I've found that some of the best developers of all are
    > English majors. They'll often graduate with no programming
    > experience at all, and certainly without a clue about the
    > difference between DRAM and EPROM.
    >
    > But they can write. That's the art of conveying
    > information concisely and clearly. Software development
    > and writing are both the art of knowing what you're going
    > to do, and then lucidly expressing your ideas.«
    >
    > http://praisecurseandrecurse.blogspot.com/2007/03/english-majors-as-programmers.html
    >


    Wow! What a prompt reply! I bet you had it under your desk just in case..

    Well I am not Eric Raymond, neither Djikstra, nor the editor of the
    second article, but I really respect all of them...and I really hoped
    you had something to say about the C language syntax in that previous post.
    By the way...I am italian, sorry for my english... :)

    --
    Non puoi insegnare qualcosa ad un uomo. You cannot teach a man anything.
    Lo puoi solo aiutare -- Galileo Galilei -- You can only help him
    a scoprirla dentro di sé. discover it in himself.

    Vincenzo Mercuri
    Vincenzo Mercuri, Nov 26, 2010
    #10
  11. Sandy Beech

    Sjouke Burry Guest

    Stefan Ram wrote:
    > Barry Schwarz <> writes:
    >> On Fri, 26 Nov 2010 00:11:30 -0800 (PST), Jim Dude
    >> <> wrote:
    >>>> printf("what is you're name:\n");
    >>>> printf("enter you're date of birth (mm/dd/yy):\n");
    >>> There are important syntax errors in both lines.

    >> Really? Would you care to elaborate?

    >
    > It seems as if that should be:
    >
    > printf( "What is your name?\n" );
    > printf( "Enter your date of birth (mm/dd/yy):\n" );
    >
    > .
    >

    Whenever we meet, remind me not to hire you, not even as a programmer.
    Sjouke Burry, Nov 26, 2010
    #11
  12. Sjouke Burry wrote:
    > Stefan Ram wrote:
    >> Barry Schwarz <> writes:
    >>> On Fri, 26 Nov 2010 00:11:30 -0800 (PST), Jim Dude
    >>> <> wrote:
    >>>>> printf("what is you're name:\n");
    >>>>> printf("enter you're date of birth (mm/dd/yy):\n");
    >>>> There are important syntax errors in both lines.
    >>> Really? Would you care to elaborate?

    >>
    >> It seems as if that should be:
    >>
    >> printf( "What is your name?\n" );
    >> printf( "Enter your date of birth (mm/dd/yy):\n" );
    >>
    >> .
    >>

    > Whenever we meet, remind me not to hire you, not even as a programmer.


    That seems a harsh reaction to someone showing himself capable of
    working out what an earlier poster was getting at.
    J. J. Farrell, Nov 26, 2010
    #12
    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. Franz

    Page_Load is skipped

    Franz, Mar 5, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    371
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Mar 5, 2006
  2. Remy Blank
    Replies:
    8
    Views:
    350
    Jeremy Fincher
    Sep 24, 2004
  3. Remy Blank
    Replies:
    0
    Views:
    450
    Remy Blank
    Sep 24, 2004
  4. FerrisUML

    scanf for char input getting skipped

    FerrisUML, Sep 18, 2008, in forum: C Programming
    Replies:
    9
    Views:
    760
  5. FerrisUML

    scanf for char input getting skipped

    FerrisUML, Sep 18, 2008, in forum: C Programming
    Replies:
    0
    Views:
    389
    FerrisUML
    Sep 18, 2008
Loading...

Share This Page