Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Skipped input

Reply
Thread Tools

Skipped input

 
 
Sandy Beech
Guest
Posts: n/a
 
      11-25-2010
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);
}
 
Reply With Quote
 
 
 
 
Stefan Ram
Guest
Posts: n/a
 
      11-25-2010
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.«

 
Reply With Quote
 
 
 
 
Barry Schwarz
Guest
Posts: n/a
 
      11-25-2010
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
 
Reply With Quote
 
Seebs
Guest
Posts: n/a
 
      11-26-2010
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 / usenet-
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.
 
Reply With Quote
 
Jim Dude
Guest
Posts: n/a
 
      11-26-2010
> * 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)
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      11-26-2010
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
 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      11-26-2010
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" );

.

 
Reply With Quote
 
Kenny McCormack
Guest
Posts: n/a
 
      11-26-2010
In article <your->,
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" );
>
> .
>


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)

 
Reply With Quote
 
Stefan Ram
Guest
Posts: n/a
 
      11-26-2010
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.blogspo...ogrammers.html

 
Reply With Quote
 
Vincenzo Mercuri
Guest
Posts: n/a
 
      11-26-2010
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.blogspo...ogrammers.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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
scanf for char input getting skipped FerrisUML C Programming 9 09-29-2008 04:42 PM
scanf for char input getting skipped FerrisUML C Programming 0 09-18-2008 02:28 AM
Page_Load is skipped Franz ASP .Net 1 03-05-2006 08:40 PM
unittest.py patch: add skipped test functionality Remy Blank Python 0 09-24-2004 09:08 AM
unittest: new reporting category "skipped" Remy Blank Python 8 09-23-2004 11:47 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57