🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: alt.comp.programming
4 messages
4 total messages Started by Psybar Phreak Mon, 20 Jan 2003 09:20
need help - formatting input
#5
Author: Psybar Phreak
Date: Mon, 20 Jan 2003 09:20
23 lines
1965 bytes
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)

firstly i was thinking about using scanf("%03d, %01d, %01d", &year, &month,
&day);  but that will only work in case 1 where there's two dates

sooo....  does anyone know a way, in which i could just save the 2 inputs as
separate int's, run a check on the length of it, then, if its a date, break
it into pieces so i get the year in one variable, the month in another, and
the day in another?!??!

what do you think?  would this be the easiest way about it or am imissing
something a lot easier?

thanks everyone

daina
sydney, australia
Re: need help - formatting input
#6
Author: Psybar Phreak
Date: Mon, 20 Jan 2003 12:52
39 lines
2204 bytes
sorry - prolly should have emtnioned i was trying to do this in c

thanks

daina


"Psybar Phreak" <gotyoubacktoo@yahoo.com> wrote in message
news:3e2bbf5d$0$7813$afc38c87@news.optusnet.com.au...
> 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)
>
> firstly i was thinking about using scanf("%03d, %01d, %01d", &year,
&month,
> &day);  but that will only work in case 1 where there's two dates
>
> sooo....  does anyone know a way, in which i could just save the 2 inputs
as
> separate int's, run a check on the length of it, then, if its a date,
break
> it into pieces so i get the year in one variable, the month in another,
and
> the day in another?!??!
>
> what do you think?  would this be the easiest way about it or am imissing
> something a lot easier?
>
> thanks everyone
>
> daina
> sydney, australia
>
>
Re: need help - formatting input
#7
Author: Peter "Shaggy" Haywood
Date: Sun, 26 Jan 2003 01:17
65 lines
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"?
Re: need help - formatting input
#8
Author: 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"?
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads