Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TAXCODES 1

Status
Not open for further replies.

CCARL2

Programmer
Feb 9, 2003
2
0
0
GB
HI ALL, i am still new to pascal and i cannot figure out how to execute the following bit of code properly. i know that the answer is staring me straight in the face but i just cant see it. i can get it to execute but it doesnt work properly. also would there be an easier way of coding it? please help as i have bee on this for hours.


write('NOW ENTER TAXCODE > ');
readln(TaxCode);
if (TaxCode[1]<'0')or(TaxCode[1]>'9')and
(TaxCode[2]<'0')or(TaxCode[2]>'9')and
(TaxCode[3]<'0')or(TaxCode[3]>'9')and
(TaxCode[4]<>'h')or(TaxCode[4]<>'l')
or(TaxCode[4]<>'H')or(TaxCode[4]<>'L')then;

begin
repeat
writeln('INVALID TAXCODE!! PLEASE DO IT AGAIN.> ');
readln(TaxCode);
until(TaxCode[1]>='0')and(TaxCode[1]<='9')
and(TaxCode[2]>='0')and(TaxCode[2]<='9')
and(TaxCode[3]>='0')and(TaxCode[3]<='9')
and(TaxCode[4]='l')or(TaxCode[4]='L')or(TaxCode[4]='h')or(TaxCode[4]='H');
end;


 
Two problems,
(1) My version of pascal (I tried turbo pascal 6.0, OK, it's old, so this might not apply everywhere) cannot readln an array. You need to read each individual item of the array:

var
q : word;
taxcodes : array[0..4] of word; (or whatever)

begin
for q := 0 to 4 do
begin
readln(taxcodes[q]);
end;
end;

(2) All those ands in your first mega-if need to be ors. You want it to object if any of the tax codes is wrong, not if all of them are wrong.

(3) Combining this, the best thing would be to put the test for invalid tax code in the input loop, so it only needs to test the current value and not worry about the rest of the array:

const
sillycode = 9999;

...later...
for q := 0 to 4 do
begin
taxcodes[q] := sillycode;
while (taxcodes[q] < 0) or (taxcodes[q] > 9) do
begin
readln(taxcodes[q]);
end;
end;

.. or somethign like that.

Good luck
 
This is what I would do...

[tt]
program taxcodes;

function IsTaxCode(aCode:string):boolean;
const
Digits = ['0'..'9'];
HiLoChars = ['H', 'L', 'h', 'l'];
begin
IsTaxCode:= ( Length(aCode) = 4 )
and ( aCode[1] in Digits )
and ( aCode[2] in Digits )
and ( aCode[3] in Digits )
and ( aCode[4] in HiLoChars );
end;


var
TaxCode:string;
begin
write('Enter tax code: ');
readln(TaxCode);
if ( not IsTaxCode(TaxCode) ) then repeat
writeln('Invalid code, please try again.');
write('Enter tax code: ');
readln(TaxCode);
until IsTaxCode(TaxCode);
end.

[/tt]
 
ah, sorry, I didn't notice the h/l bit of the code and misinterpreted your string as an array.
Shame, shame, huge apologies, next time I'll read the the question properly.
 
thanks for all your help guys. ppc386 especially. this bit of code does work on its own but if i am to insert it into a program, where exactly do i put everything. for instance, this is my first dealing with a 'function' and also with 'boolean' i do know all the boolean gates and what they do but i have never played with them. please help me.
TA MUCHLY GUYS
 
It sounds like you have more questions than I can answer here.

I would recommend checking out a book on Pascal, maybe this one:

&quot;Computer Programming in PASCAL the Easy Way&quot;
Author: Downing/Yoshimi
Publisher: Barron's Educational Series


Or browse some of the online tutorials:
 
OK, seeing as I made such a mess of replying before, I'll have a go at doing better this time.

(1) Functions

Functions are dead easy. They are just like procedures, except they return a value. Therefore when you declare a function, you have to state that it is a function,
and what sort of value it returns. Here are some examples:

procedure MyProc(a, b : word);
function MyFunc(a, b : word) : longint;

Both expect to be called with two parameters (words) in brackets after them, but the second will return a long int value.

Functions return a single value. For example, you can't return two words and a pointer in the same way as you can pass two words and a pointer. Pity.

So when you call a function, obviously you will (normally) want to call it in a way that collects the value, so instead of writing
MyProc(1, 2)
you will call it as
MyVar := MyFunc(1, 2);

Now inside the code of the function you have to make clear what's being returned. This isn't done by &quot;return&quot; or anything like that. Instead what you do is assign the value to the function name, treating the function name as though it were a variable.

function MyFunc(a, b : word) : longint;
begin
MyFunc := a + b;
end;

(2) Booleans

boolean is simply a variable type holding the values true or false. ppc386's function returned one of these. Everything you know about boolean algebra applies; the boolean variables and functions are simply the bits and pieces you need to do boolean algebra in the same way as other variables and functions are what you need to do arithmetic.

(3) Where do I put ppc386's code?

You can put the function declaration anywhere you want provided it is either (a) before the procedures/functions that call it, or (b) declared in advance, or (c) put in a unit and declared in the &quot;interface&quot; section, and this unit is refered to in the &quot;uses&quot; section of any unit that wants to call ppc386's function.

Otherwise I agree with ppc386 about books. Getting pascal books is often not easy in provincial bookshops (pascal being considered a dead language), but libraries will have something.
 
Nice job, lionhill -
Wish I could be that good at explaining things...
 
ppc386,

It might be an idea to post the above book and websites (on learning Pascal) into an FAQ for this forum. I've done the same for the Delphi forum: faq102-2794

Cheers, Clive [infinity]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top