Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!mips!atha!aunro!alberta!brazeau.ucs.ualberta.ca!unixg.ubc.ca!ubc-cs!uw-beaver!ssc-vax!sml From: sml@ssc-vax (Stuart Lewis) Newsgroups: comp.lang.pascal Subject: Re: Third TPascal Keywords: puzzle,pascal Message-ID: <4223@ssc-bee.ssc-vax.UUCP> Date: 28 Jun 91 18:19:02 GMT References: <1991Jun27.131018.26430@informatik.uni-erlangen.de> Sender: news@ssc-vax.UUCP Reply-To: sml@ssc-vax.UUCP (Stuart Lewis) Organization: Boeing Aerospace & Electronics Lines: 123 In article <1991Jun27.131018.26430@informatik.uni-erlangen.de> prknoerr@immd4.informatik.uni-erlangen.de (Peter Knoerrich) writes: >Ok, here you are, the third one: > >This one needs some warnings, for it will for sure not run on >any compiler. For instance, you need a word data type which >is not implemented on most unix pc compilers (for some good >reason, agreed). > >But I think most of you guys either use Borland's TurboPascal >or perhaps some other pascal which does have unsigned data types, >so you will perhaps be able to follow this one (I know I'm >advertising, but at least it's for a reasonable product I do it for) > I'm not sure I understand what you're telling me here about the word data type. But then, I program on the VAX - not on pc's (not until this fall anyway). Given that forewarning about my lack of pc Pascal, let me approach your code from a standard VAX Pascal point of view....... >program AdjustValue; > (* the var declaration must be here - not after the procedure *) (* that *immediately* will give compile time errors *) VAR (* from below *) given : word; (* from below *) >procedure AdjustAndTrim(var value : word;change : integer); (* ^^^^^^ since "change" is always -1, it would be 'tidier' to declare it as a constant up above and not worry about passing it *) > var > temp : integer; > begin > temp := value + change; (* I don't know what TYPE "word" is (pc exclusive?), but this last line will not work unless "value" (VAR'd "given") is ultimately either an INTEGER value or CHAR value, because "temp" is declared as type INTEGER. You can declare a data type word = INTEGER; up above your global VAR's, and then it would work. *) > if (temp<0) then > temp := 0 > else > if (temp>1000) then > temp := 1000; > value := temp; > end; > >var (* global VAR - delete this line - move to above *) > given : word (* ditto - plus syntax error, need ";" at end *) > >begin > given := 0; (* here again, I don't know what type "word" is, but you're assigning an INTEGER (or possibly CHAR) value to "given" - save yourself some trouble and declare it as one of those two *) > AdjustAndTrim(given,-1); >end. > [stuff deleted] > >What goes wrong is the following: Even the first assignment fails. >Though an integer has been provided to catch up with those negative >fellows, a range check error will occur when called with the given >parameters. > [stuff deleted] just a matter of personal preference or style, but here's what I would do.... PROGRAM AdjustValue (input, output); CONST change = -1; TYPE word = INTEGER; VAR given : word; PROCEDURE AdjustAndTrim (VAR value : word); VAR temp : INTEGER; BEGIN (* PROCEDURE AdjustAndTrim *) temp := value + change; IF (temp < 0) THEN value := 0 ELSE IF (temp > 1000) THEN value := 1000 ELSE value := temp; END; (* PROCEDURE AdjustAndTrim *) BEGIN (* MAIN *) write('Enter a value : '); readln(given); AdjustAndTrim(given); writeln('Adjusted value is : ', given); END. (* MAIN *) I added read & write statements - the program isn't very useful as a learning tool unless you can see what the heck it's doing! :-) Again, this is just my personal style, but it does solve the TYPE problems with "word" and "given". I didn't run this, but I'd be *real* embarassed if it crashed on somebody! >--- >sig? You must be kidding with all those SIGKILLs around on this machine! >Peter Knoerrich, email: prknoerr@faui43.informatik.uni-erlangen.de Stuart Lewis sml@ssc-vax