Article View: alt.comp.programming
Article #7Re: need help - formatting input
From: Peter "Shaggy" Haywood
Date: Sun, 26 Jan 2003 01:17
Date: Sun, 26 Jan 2003 01:17
65 lines
4836 bytes
4836 bytes
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:
<3e31e1d6.2598846@news.alphalink.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!newsfeed-west.nntpserver.com!hub1.meganetnews.com!nntpserver.com!telocity-west!DIRECTV!news-out.spamkiller.net!propagator2-maxim!news-in.spamkiller.net!news.alphalink.com.au!news.alphalink.com.au!not-for-mailReferences:
<3e2bbf5d$0$7813$afc38c87@news.optusnet.com.au>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 108717,e57a7de65360e87c X-Google-Attributes: gid108717,public X-Google-Thread: fb361,e57a7de65360e87c X-Google-Attributes: gidfb361,public X-Google-Thread: 10dce5,e57a7de65360e87c X-Google-Attributes: gid10dce5,public X-Google-ArrivalTime: 2003-01-25 17:17:00 PST From: phaywood@alphalink.com.au.STOP.SPAM (Peter "Shaggy" Haywood) Newsgroups: alt.comp.programming,aus.students,comp.programming Subject: Re: need help - formatting input Date: Sun, 26 Jan 2003 01:17:22 GMT Reply-To: phaywood@alphalink.com.au.REMOVE.THIS.BIT Message-ID: <3e31e1d6.2598846@news.alphalink.com.au> References: <3e2bbf5d$0$7813$afc38c87@news.optusnet.com.au> X-Newsreader: Forte Free Agent 1.11/32.235 NNTP-Posting-Host: d22-ds3-mel.alphalink.com.au X-Original-NNTP-Posting-Host: d22-ds3-mel.alphalink.com.au X-Trace: news.alphalink.com.au 1043543786 d22-ds3-mel.alphalink.com.au (26 Jan 2003 12:16:26 +1100) Organization: Client of Alphalink Australia Pty. Ltd. Lines: 65 X-Admin: admin@alphalink.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!newsfeed-west.nntpserver.com!hub1.meganetnews.com!nntpserver.com!telocity-west!DIRECTV!news-out.spamkiller.net!propagator2-maxim!news-in.spamkiller.net!news.alphalink.com.au!news.alphalink.com.au!not-for-mail