Turbo Pascal Mentor wanted - apply within
Results 1 to 15 of 15

Thread: Turbo Pascal Mentor wanted - apply within

  1. #1
    Driver Terrier NooNoo's Avatar
    Join Date
    Dec 2000
    Location
    UK
    Posts
    31,824

    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."

  2. #2
    Senior Member - 1000+ Club Outcoded's Avatar
    Join Date
    Apr 2001
    Location
    Somewhere in the UK, never quite sure where
    Posts
    1,689
    [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

  3. #3
    Geezer confus-ed's Avatar
    Join Date
    Jul 1999
    Location
    In front of my PC....
    Posts
    13,087
    [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.

  4. #4
    Driver Terrier NooNoo's Avatar
    Join Date
    Dec 2000
    Location
    UK
    Posts
    31,824
    [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."

  5. #5
    Geezer confus-ed's Avatar
    Join Date
    Jul 1999
    Location
    In front of my PC....
    Posts
    13,087
    Quote Originally Posted by NooNoo
    Ed you missed the point
    ... There was I trying to be helpful ...
    Well if you are only gonna tell me half a tale ! ...

    check = 0 (by default)
    Let me first correct a misconception that you have, if you don't set a value for 'check' it mightn't contain '0' (it could have any old 'value' all you did was 'type' it - make it an integer or whatever) you have to set all variables to be 'certain' what they contain ...

    Why for art thou using 'while' to do this ?

    You could use plain old 'IF' with an 'ELSE' or even 'CASE' much better & clearer ... & does this 'pascal' have 'endif' as a Reserved word ? If so methinks that perhaps the 'while' statement got confus-ed on where its 'end' was (even if it didn't - I AM !) ...

    & why for art thou using an integer to keep track of a condition that must be either true or false, use a boolean !

    'Check' can't be a reserved word or it(the compiler) would tell you

    No doubt there's more to come with this, but I'll see what you have to say about that little lot first

    Can you post me a syntax guide for this particular 'incantation' - methinks it'll help (me if no-one else) muchly

  6. #6
    Driver Terrier NooNoo's Avatar
    Join Date
    Dec 2000
    Location
    UK
    Posts
    31,824
    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."

  7. #7
    Registered User CodeDragon's Avatar
    Join Date
    Oct 2000
    Location
    Gobbinland
    Posts
    196
    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

  8. #8
    Driver Terrier NooNoo's Avatar
    Join Date
    Dec 2000
    Location
    UK
    Posts
    31,824
    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."

  9. #9
    Geezer confus-ed's Avatar
    Join Date
    Jul 1999
    Location
    In front of my PC....
    Posts
    13,087
    So the 'while' wasn't confus-ed by the end's in question but semicolons ! ... d'uh !

    Pascal uses the semicolon as a statement separator (when it doubt miss it out - the compiler should tell you about missing ones, however 'extra' ones can cause much confus-ed-ness as we've just seen ) so begin shouldn't want one immediately after it, but 'mostly' end will - here the 'while..do' construct (which is efectively a 'begin') had become seperated by the semicolon from what it was supposed to do ...

    But that's one 'iffy' compiler - I just tried that in delphi & it told me straight away that my 'while' had no instructions following it, seems like that'll be something to live with & be aware of for next time ...

  10. #10
    Registered User CodeDragon's Avatar
    Join Date
    Oct 2000
    Location
    Gobbinland
    Posts
    196
    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:

    Code:
    begin
    then
    do
    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

  11. #11
    Geezer confus-ed's Avatar
    Join Date
    Jul 1999
    Location
    In front of my PC....
    Posts
    13,087
    ... Top answer CD ... you are in charge now of all programming teaching ... the curse of the pascal semicolon explained (Fully!) -

  12. #12
    Senior Member - 1000+ Club Outcoded's Avatar
    Join Date
    Apr 2001
    Location
    Somewhere in the UK, never quite sure where
    Posts
    1,689
    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

  13. #13
    Driver Terrier NooNoo's Avatar
    Join Date
    Dec 2000
    Location
    UK
    Posts
    31,824
    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."

  14. #14
    Registered User CodeDragon's Avatar
    Join Date
    Oct 2000
    Location
    Gobbinland
    Posts
    196
    Quote Originally Posted by NooNoo
    Code Dragon, printed and in my file, thank you.
    You're welcome.

    Quote 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

  15. #15
    Registered User deepblu's Avatar
    Join Date
    Mar 2003
    Location
    Portland, Oregon
    Posts
    52
    Quote 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

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •