Board index » delphi » Const and arrays of records

Const and arrays of records

Greetings to all

I am busy with a situation where an array of records are used in a constant
situation.

I.E.

type monthrec = record
                            month : string [10];
                            days : 1 .. 31;
                            end;

montharrays : array [1..12] of monthrec;

const montharr : montharrays = ((month : 'January';days : 31),
                                                      (month :
'february';days : 29){ ...... until December});

Now here is the question. How do I access these values in the constant. Say
I want to ask the user to enter the date in the form of YYYYMMDD and then
this will check if the date is valid.

Writeln ('enter date');
readln (d);
val (copy (d,5,2),num,err);
if num = {   NOW WHAT}

I hope you will see my problem as you can see above. I will appreciate it if
somebody can help me and put me on the right track. The book I am currently
working through, does not give a example to show you how to do it.

Thanks in advanced.

Riaan

 

Re:Const and arrays of records


Riaan Stopforth kirjoitti viestiss?
<8ekpb9$bi...@ctb-nnrp1.saix.net>...

Quote

>Writeln ('enter date');
>readln (d);
>val (copy (d,5,2),num,err);
>if num = {   NOW WHAT}

You have to extract first the month number and verify it to be in
range of 1 to 12.  Let's say you put it in a variable mnum.

Then you could extract the day number into a variable num as you do.

Now you can test it like this:

if (num >= 1) and (num <= montharr[mnum].days) then {it's ok};

--
Raimo Suonio, Helsinki, Finland
Oikeinkirjoitusohjeita news- ja web-kirjoittajille:
http://www.dlc.fi/%7Eexp-1/oikeinkirjoitus.html
http://www.dlc.fi/%7Eexp-1/suonio/

Re:Const and arrays of records


Quote
Riaan Stopforth wrote:

> I am busy with a situation where an array of records are used in a constant
> situation.

> type monthrec = record
>                             month : string [10];
>                             days : 1 .. 31;
>                             end;

> montharrays : array [1..12] of monthrec;
> const montharr : montharrays = ((month : 'January';days : 31),
>                                                       (month :
> 'february';days : 29){ ...... until December});

> Now here is the question. How do I access these values in the constant. Say
> I want to ask the user to enter the date in the form of YYYYMMDD and then
> this will check if the date is valid.

> Writeln ('enter date');
> readln (d);
> val (copy (d,5,2),num,err);
> if num = {   NOW WHAT}

> I hope you will see my problem as you can see above. I will appreciate it if
> somebody can help me and put me on the right track. The book I am currently
> working through, does not give a example to show you how to do it.

Well, it's simple enough to check "num" to see if it's between 1 and 12;
if so, then it's valid.  Do this before attempting to access the array.

Here's an example on accessing the array by printing the #of days in the
variable month "num" (assuming it's a number between 1 and 12):

writeln('#days in ',montharr.month,' is ',montharr.days);

Other Threads