Skipped input

S

Sandy Beech

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);
}
 
S

Stefan Ram

Sandy Beech said:
"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.«
 
B

Barry Schwarz

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

Seebs

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
 
J

Jim Dude

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

Stefan Ram

Barry Schwarz said:
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" );

.
 
K

Kenny McCormack

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

Stefan Ram

Vincenzo Mercuri said:
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
 
V

Vincenzo Mercuri

Stefan said:
»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
 
S

Sjouke Burry

Stefan said:
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.
 
J

J. J. Farrell

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

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,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top