-
November 15th, 2003, 09:43 AM
#1
Driver Terrier
Turbo Pascal Mentor wanted - apply within
Ok, I am learning Turbo Pascal as part of my degree course.
What I would like to be able to do is discuss approaches and pascal's *cough* features without actually asking for code to solve the problem. This means the applicant must be able to guide rather than solve. For instance, my current dilemma is a simple error trap
Code:
Program baseconv; uses wincrt;
var check:integer ;
var f,g:string;
Begin
CursorTo(0,2);
writeln('This program is designed for the sole purpose of');
write('Converting a number of your chosen base to denary');
cursorTo (0,6);
{here be dragons, see post below....}
while check=1 do;
begin;
write('Please enter the base you want to convert to denary');
readln(f);
writeln('Now the number you wish to convert (whole numbers only please!)') ;
readln(g);
writeln(pos(f,g));
if (pos(f,g)=1) then
begin;
writeln ('Sorry you said base ',f,' the number cannot contain ',f);
f:=' ';
g:=' ';
end;
check:= pos(f,g) ;
writeln(check) ;
end;
end.
OK, all I want to do is have a while do loop that continues until the user makes a valid input, at which point check:=0 and the rest of the program will execute.
Now, the problem is if I put in check:=1 - the program hangs at this point
If I leave check:=1 out, it still does the while do loop even though check actually equals 0...
Some one put me out of my misery, what fundamental trap have I sprung, or what trick do I need to get pascal to behave?
Last edited by NooNoo; November 15th, 2003 at 09:56 AM.
Never, ever approach a computer saying or even thinking "I will just do this quickly."
-
November 15th, 2003, 02:37 PM
#2
Senior Member - 1000+ Club
[QUOTE=NooNoo]Ok, I am learning Turbo Pascal as part of my degree course.
This is going to sound kinda nuts, but.
Set that var to something other than 0 or 1, eg 4.
I'm in charge and I say we blow it up
-
November 16th, 2003, 08:49 AM
#3
Geezer
[Begin 'possible annoying advice' mode]Denary =decimal ! = base 10
lesson 1 ... use the simplest words ! so as not to compound any confus-ed-ness
2 ... pleae add as many comments as possible to all code so as to make readable to the 'syntax incontinent' {use them curly brackets frequently} who then have to look it all up having not done any 'ordinary pascal' programming for many, many yonks
3 ... Give variables 'meaningful' names allways (unless optomising already 'good' code) - so instead of 'f' 'Required_Base' for example
4 .... TRY and write RESERVED words (Programming language,operands & such) in capital letters, makes life easier
5 .... Please don't post so it falls off the page ! [end mode]
POS seems to be your problem along with some type mismatching (& do you infact have enough variables to do what you want ?), I think that gets string position from a larger string, but what's the syntax ? I dunno !!!
So I looked ....
Syntax: Location := Pos(substr, mystr)
where substr is the substring being looked for and
mystr is the string being searched
Pos returns as an integer the location (subscript) of where substr was found to start in mystr.
Substr : string;
MyStr : string;
Location : integer;
SubStr := 'don';
MyStr := 'Macdonald';
Location := Pos(SubStr, MyStr)
As a result of the above code, Location will hold the value 4
& applying that logic we see (or I do) that POS wouldn't appear to be the way to do it ... if we for instance wanted to convert '1e' in Hex (base 16) to decimal what good is it knowing that '16' isn't in the string '1e' or whichever way around it is !!????
You first need to check that your string type 'inputs' are valid integers, Try using STRING or STR(I think that's the one for 'turbo' pacal) on the inputs (this seems mighty hard for a first assignment- I'm about to use the dirty phrase 'exception handlers') if the conversion fails, it'll create an 'exception' for which you need a variable to process it & some kind of routine which happens when the error occurs...
I'm getting the feeling I might be overcomplicating this muchly !!! so I'll stop typing & let you (Noo) come back on the points thus far ....
Last edited by confus-ed; November 16th, 2003 at 08:52 AM.
-
November 16th, 2003, 09:50 AM
#4
Driver Terrier
[Begin 'possible annoying advice' mode]Denary =decimal ! = base 10
Title programs and their comments so they relate to the language used in the assignment paper.
"for this assignment use the following variables...."
Ed you missed the point.... its not the POS thats the problem, I was toying with that to find out what it does and how, that code actually works...
Its the looping I gots me a problem with...
Check is an integer, if you set it to 1 just before the while do loop, the proggie hangs, if you leave out the statement, check = 0 (by default) but it still does the while check=1 do loop, even though check=0
I cant find check listed in reserve words - we are using Borland Turbo Pascal for Windows, 1.5 (yah, real uptodate ... I know I know....)
Never, ever approach a computer saying or even thinking "I will just do this quickly."
-
November 16th, 2003, 10:39 AM
#5
Geezer
-
November 16th, 2003, 10:44 AM
#6
Driver Terrier
While do loop so that the user controls how many times theh program is run... the idea being if s/he wants to do the process again or not s/he inputs y or n to a question, which would reset the check variable accordingly.
closest is www.freepascal.org - is pretty close.
back to looping, I did check that check was equal to 0
I also tried looping with char variable, but when I evaluated the contents of the reply with the ord() function, it told me ascii 13 - which is cr - and not the 'y' that I had put in....
I figure I am missing something very fundamental here....
Never, ever approach a computer saying or even thinking "I will just do this quickly."
-
November 16th, 2003, 10:52 AM
#7
Registered User
Noo, is that your code "as is", copied and pasted?
Cos you've got
Code:
while check=1 do;
begin;
Neither of which should have semicolons, thus:
Code:
while check = 1 do
begin
writeln(check);
writeln('Like so');
end;
Not sure that that's the problem, but if that is the way your code is written, your program's probably getting as far as "do" and giving up.
I'd also suspect that this is why you're having the loop execute when "check" <> 1, since it'll just see the code as being outside the loop.
Having said that, I would have expected the TP compiler to throw a wobbler about program syntax if this were the case.
Other than that, as has been said, you really should initialise "check" before you use it, otherwise interesting things (or nothing) could happen.
Hope this helps.
CD
There are only two truly infinite things, the universe and stupidity. And I am unsure about the universe. - Albert Einstein
-
November 16th, 2003, 01:01 PM
#8
Driver Terrier
Code Dragon, that got it, ; in the wrong place wreaks havoc, and no the compiler doesn't bat an eyelid. Is there a rule somewhere about this stupid ; business? (Because the course tutor is very inconsistant about this, his examples are always missing one or have and additional one somewhere)
Never, ever approach a computer saying or even thinking "I will just do this quickly."
-
November 16th, 2003, 02:00 PM
#9
Geezer
-
November 16th, 2003, 08:50 PM
#10
Registered User
I'd agree with confus-ed on this one - when in doubt.
Basically, each operation statement (e.g. function call, assignment, etc.) should end with a semicolon:
Code:
A := 1;
Writeln('Hello Noo');
clrscr;
Similarly, operation prototypes and/or definitions should end with a semicolon:
Code:
procedure SayHello(Name: String);
function PersonExists(Name: String): Boolean;
On the other hand, certain reserved words don't require a semicolon:
Because they don't come at the end of a block of code. For example:
Code:
if Pi = 3.176 then
writeln('Okiedokie');
if Pi <> 3.178 then
begin
writeln('Are you sure this is Pi? ' + Pi);
writeln('I don't think it is!');
end;
Note here that end requires a ';' because it marks the end of a logical block of code. There are exceptions to this rule, such as at the end of a program where end should be followed by a full stop/period thus:
Code:
program test
begin
{ Do some schtuff }
end.
The only time you need to watch out for semicolons is in if...then...else... statements. The (rather abstruse) rule here is that the statement preceeding the else should not be closed by a semicolon. Thus:
Code:
if Pi = 3.17 then
writeln('Okay') {miss the semicolon here}
else
writeln('Our survey said "Errr!". Try again!');
{ This is true for if...then..begin..end...else statements too: }
if Pi <> 3.178 then
begin
writeln('Are you sure this is Pi? ' + Pi);
writeln('I don't think it is!');
end {miss the semicolon here}
else
writeln('Okay');
The compiler should pick you up on semicolons before "else" clauses, but bear in mind that I'm used to using Delphi rather than TP, so my knowledge of the TP compiler is a little rusty.
If there's any other way I can help, let me know.
CD
There are only two truly infinite things, the universe and stupidity. And I am unsure about the universe. - Albert Einstein
-
November 16th, 2003, 09:22 PM
#11
Geezer
-
November 17th, 2003, 09:02 PM
#12
Senior Member - 1000+ Club
Damn those semi-colons.
Welcome to programming noo - they'll drive you ****ing nuts for years to come
I'm in charge and I say we blow it up
-
November 18th, 2003, 06:36 PM
#13
Driver Terrier
Code Dragon, printed and in my file, thank you.
IJ... not as much as our damn tutor is going to grrrrr.
Good laugh today, as an example of boolean and not statements, he used a repeat until loop with a variable call reply and went through this entire 20 mins about how you had to check for Y and y and how De Morgan's law operated....
Me being me said, why not save the effort and just upper or lower case any user input?
Never, ever approach a computer saying or even thinking "I will just do this quickly."
-
November 18th, 2003, 06:53 PM
#14
Registered User
Originally Posted by NooNoo
Code Dragon, printed and in my file, thank you.
You're welcome.
Originally Posted by NooNoo
Me being me said, why not save the effort and just upper or lower case any user input?
There's one smarta$$ in every class isn't there? (I should know - in my class it was me ).
Just hope that you don't get to the point of teaching the teacher. That's embarrassing as well as more work than you need to deal with.
There are only two truly infinite things, the universe and stupidity. And I am unsure about the universe. - Albert Einstein
-
December 11th, 2003, 08:38 AM
#15
Registered User
Originally Posted by NooNoo
Code Dragon, printed and in my file, thank you.
IJ... not as much as our damn tutor is going to grrrrr.
Good laugh today, as an example of boolean and not statements, he used a repeat until loop with a variable call reply and went through this entire 20 mins about how you had to check for Y and y and how De Morgan's law operated....
Me being me said, why not save the effort and just upper or lower case any user input?
Their actually is a function for that in Turbo Pascal that goes something like this : x:=UpCase(y)
Whereas :
Var ch : Char;
done : Boolean;
Repeat
read(kbd,ch);
done = ( UpCase(ch) = 'Y' )
Until done;
Last edited by deepblu; December 13th, 2003 at 07:41 AM.
Could not hit the curveball
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|
Bookmarks