Reading Diff. Var types 
Author Message
 Reading Diff. Var types

: I am using Turbo Pascal 6.0 and am trying to read a series of Variables
: of a Date then outputing the Date in a different format.

: Can Anyone help me out with why this does not work?

: Program Date;

: var

:  Month, Day, Year : int;
:  Slash1, Slash2 : char;

: begin

: Writeln ('Enter the Date');
: Readln (Month, Slash1, Day, Slash2, Year);
: Writeln (Day, Slash1, Month, Slash2, Year);
: [...]

You need to read the date as a _single_ string, then parse it
and extract Day, Month, Year.    



Wed, 18 Jun 1902 08:00:00 GMT  
 Reading Diff. Var types
I am using Turbo Pascal 6.0 and am trying to read a series of Variables
of a Date then outputing the Date in a different format.

Can Anyone help me out with why this does not work?

Program Date;

var

 Month, Day, Year : int;
 Slash1, Slash2 : char;

begin

Writeln ('Enter the Date');
Readln (Month, Slash1, Day, Slash2, Year);
Writeln (Day, Slash1, Month, Slash2, Year);

End.

        I am getting invalid numeric format when the program tries to
read the Date.

Any Ideas on this would be peachy.

Thanks
Keith.



Wed, 18 Jun 1902 08:00:00 GMT  
 Reading Diff. Var types

Quote:

>I am using Turbo Pascal 6.0 and am trying to read a series of Variables
>of a Date then outputing the Date in a different format.
>Can Anyone help me out with why this does not work?
>Program Date;
>var
> Month, Day, Year : int;
> Slash1, Slash2 : char;
>begin
>Writeln ('Enter the Date');
>Readln (Month, Slash1, Day, Slash2, Year);
>Writeln (Day, Slash1, Month, Slash2, Year);
>End.
>    I am getting invalid numeric format when the program tries to
>read the Date.
>Any Ideas on this would be peachy.

Alas, Turbo Pascal is not smart enough to say, oh, there's a slash, so
I'll just stop reading in a number, and leave it in the keyboard
buffer.  Your best bet is to read things in one character at a time,
and check for the slashes as you go (and you must also build the
numbers too).  This method is also much more bullet proof, but
time-consuming.

Mark



Wed, 18 Jun 1902 08:00:00 GMT  
 Reading Diff. Var types

Quote:
>I am using Turbo Pascal 6.0 and am trying to read a series of Variables
>of a Date then outputing the Date in a different format.
>Program Date;
>var
> Month, Day, Year : int;
> Slash1, Slash2 : char;
>begin
>Writeln ('Enter the Date');
>Readln (Month, Slash1, Day, Slash2, Year);
>Writeln (Day, Slash1, Month, Slash2, Year);
>End.
>        I am getting invalid numeric format when the program tries to
>read the Date.

     It worked for me, but then I'm not using Borland.  I suspect that's your
problem.  Certainly, a good implementation of "read" and "readln" would parse
the input string, stopping when it sees the first non-numeric character.  In
all fairness, this is probably something that is not fully spelled out in the
Pascal Standard, so maybe it is unfair to fault Borland.

     The solution for you is probably to read in the entire string and parse
it yourself.  What a pain!

Bob Schor
Pascal Enthusiast



Wed, 18 Jun 1902 08:00:00 GMT  
 Reading Diff. Var types

Quote:
> I am using Turbo Pascal 6.0 and am trying to read a series of Variables
> of a Date then outputing the Date in a different format.

> Can Anyone help me out with why this does not work?

> Program Date;

> var

>  Month, Day, Year : int;
>  Slash1, Slash2 : char;

> begin

> Writeln ('Enter the Date');
> Readln (Month, Slash1, Day, Slash2, Year);
> Writeln (Day, Slash1, Month, Slash2, Year);

> End.

>    I am getting invalid numeric format when the program tries to
> read the Date.

> Any Ideas on this would be peachy.

UNfortunately, this is one place where C is better than Pascal, where its
file I/O is better.  In C you could write this as:

scanf("%d/%d/%d", month, day, year);

However, this is a pascal discussion group and we need to figure out an
elegant solution.  What you can do is find where the slashes are, break
the string into three parts based on that, and use the val() procedure to
convert them to values.  This can all be done, in fact, without rewriting
the read functions (although that's a feasible solution too except that
you lose pretty much all control over it).  SO:

Pascal has a function pos which reports the position of a substring
(including a char) in a larger string.  With this we can split the date
string into two smaller strings (month and day/year) and then split
day/year into day and year.  let's try some code:

var
  mdy : string; { Our composite string }
  month, day, year : string;
  p, l : byte; { the position and length }

begin
  write ('M/D/Y: ');
  readln (mdy);

  p := 1; { start of string }
  l := pos('/', mdy) - p;
  month := copy(mdy, p, l);
  delete (mdy, p, l); { remove month from mdy }

  p := 1; { again, start of string, this is just for good form here }
  l := pos('/', mdy) - p;
  day := copy(mdy, p, l);
  delete (mdy, p, l); { remove day from mdy }

  year := mdy; { year is all that's left }

  writeln ('D/M/Y: ', day, '/', month, '/', year);
end.

If you have any more questions, feel free to ask.

Quote:
> Thanks
> Keith.

-----------------\------------------------------------------------------------
Quantum Porcupine \-\  People kept on telling me to get on with it and write
a.k.a. Joshua Shagam > my damn .sig file.  Well I finally got around to it

----------------/-------------------------------------------------------------
  Missing Link / Puuttuva Lenkki


Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. type of VAR parameter

2. Types of actual and formal var parameters must be identical

3. Var Word to Var String

4. newbee question,string var to char var

5. Reading typed files

6. (FPU) EXTENDED type read by C program

7. Reading database-type files

8. Reading Blockwritten typed files using C

9. Reading Blockwritten typed files using C

10. Need to read type 0 PCX file in TP30

11. Read lots of records at the same time from a typed file

12. Reading Enumerated Types

 

 
Powered by phpBB® Forum Software