🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: alt.comp.programming
Article #8

Re: need help - formatting input

#8
From: Alan
Date: Mon, 27 Jan 2003 16:34
74 lines
4920 bytes
This solution will be correct for one type of date formation.
Any other solutions ?

"Peter "Shaggy" Haywood" <phaywood@alphalink.com.au.STOP.SPAM> wrote in
message news:3e31e1d6.2598846@news.alphalink.com.au...
> Groovy hepcat Psybar Phreak was jivin' on Mon, 20 Jan 2003 20:20:28
> +1100 in comp.programming.
> need help - formatting input's a cool scene! Dig it!
>
> >hi all....  hope someone can lend me a  little help on a problem im
working
> >on
> >
> >background desc :  program can take either
> >1)  Two dates in the format (yyyymmdd) through stdin, or
> >2) One date in the format (yyyymmdd) and a single number (up to 7 digits
> >long)
>
>   And how are you supposed to distinguish between these two cases?
> (Don't tell me; I know already. But I'm making a point.) Here's a
> clue: there are 8 digits in the specified date format, but your
> non-date number is up to 7 digits.
>   If only there were a way to count the number of consecutive digits
> in the input text...! But for that, we'd need to read it in as a
> string, would we not?
>   What functions do we have at our disposal which would allow us to do
> this? We have gets(), but that is not recomended. AAMOF, it is
> strongly recomended you avoid gets() like the plague. We have scanf().
> That can be used safely and well, if you know how. Unfortunately few
> newbies know the pitfalls well enough to do so, and even many
> experienced programmers avoid it and recomend others do the same. We
> also have fgets(). BINGO! That's just what we need. Look it up in your
> favourite C manual.
>   Next, we need a way to determine the number of digits in the string
> we read in. What functions do we have at our disposal for this? Well,
> none. We'll have to write our own. This is pretty easy. All we need do
> is to loop through the characters in the string and stop when we hit a
> non-numeric character. (Note that the null character that terminates a
> string is non-numeric. So that stops us at the end of the string.) But
> how do we determine what is or isn't a numeric character? Are there
> any functions available for that? Yep! Look up isnum() in your manual.
>   Next, having determined the number of consecutive digits in the
> input, if this number is < 8, it's not a date. In that case use
> strtod(), atoi(), sscanf() or whatever means you prefer to parse the
> string. Otherwise, use sscanf() or whatever means you desire to parse
> the date format
>
> >firstly i was thinking about using scanf("%03d, %01d, %01d", &year,
&month,
>                                            ^^^^  ^^^^  ^^^^
>   This is wrong. Since the year is supposed to have 4 digits, and the
> month and day 2 digits each, you need to be reading in 4 then 2 then 2
> digit numbers, respectively, not 3 then 1 then 1 digit numbers,
> respectively. Also, the '0's in the above conversion specifiers are
> superfluous. They serve no useful purpose whatsoever. You can safely
> leave these out. And finally, the format string you have passed to
> scanf() will not read in three numbers if the user enters data the way
> you intend. It will try to read one number, a comma, a sequence of
> white space, another number, another comma, another sequence of white
> space then the last number. But since your date format has numbers
> that are not separated by commas and white space, this will fail. You
> want something like this:
>
> scanf("%4d%2d%2d", &year, &month, &day);
>
> --
>
> Dig the even newer still, yet more improved, sig!
>
> http://alphalink.com.au/~phaywood/
> "Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry &
W. Walker.
> I know it's not "technically correct" English; but since when was rock &
roll "technically correct"?
Technical Details
Message-ID: <3e355f93$0$7812$afc38c87@news.optusnet.com.au>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!news1.optus.net.au!optus!spool01.syd.optusnet.com.au!spool.optusnet.com.au!not-for-mail
References: <3e2bbf5d$0$7813$afc38c87@news.optusnet.com.au> <3e31e1d6.2598846@news.alphalink.com.au>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: fb361,e57a7de65360e87c
X-Google-Attributes: gidfb361,public
X-Google-Thread: 10dce5,e57a7de65360e87c
X-Google-Attributes: gid10dce5,public
X-Google-Thread: 108717,e57a7de65360e87c
X-Google-Attributes: gid108717,public
X-Google-ArrivalTime: 2003-01-27 08:35:23 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!news1.optus.net.au!optus!spool01.syd.optusnet.com.au!spool.optusnet.com.au!not-for-mail
From: "Alan" <nguyenvo@optusnet.com.au>
Newsgroups: alt.comp.programming,aus.students,comp.programming
References: <3e2bbf5d$0$7813$afc38c87@news.optusnet.com.au> <3e31e1d6.2598846@news.alphalink.com.au>
Subject: Re: need help - formatting input
Date: Tue, 28 Jan 2003 03:34:22 +1100
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1106
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106
Lines: 76
Message-ID: <3e355f93$0$7812$afc38c87@news.optusnet.com.au>
NNTP-Posting-Host: 210.49.177.152
X-Trace: 1043685267  7812 210.49.177.152