TAXCODES
TAXCODES
(OP)
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;
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;
RE: TAXCODES
(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
RE: TAXCODES
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.
RE: TAXCODES
Shame, shame, huge apologies, next time I'll read the the question properly.
RE: TAXCODES
TA MUCHLY GUYS
RE: TAXCODES
I would recommend checking out a book on Pascal, maybe this one:
"Computer Programming in PASCAL the Easy Way"
Author: Downing/Yoshimi
Publisher: Barron's Educational Series
Or browse some of the online tutorials:
http://web.mit.edu/taoyue/www/tutorials/pascal/contents.html
http://www.algonet.se/~khaan/tutor/index.html
http://www.techiwarehouse.com/Pascal/Pascal_Tutorial.html
RE: TAXCODES
(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 "return" 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 "interface" section, and this unit is refered to in the "uses" 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.
RE: TAXCODES
Wish I could be that good at explaining things...
RE: TAXCODES
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
www.kucu.co.uk