0

So below is the module of code I have created to verify a user input is between a range but this error keeps appearing below it ' statement expected but function found', any ideas anyone? Many thanks

function get_choice(var Options: integer): integer;
      var
      choice: integer;

      begin
        while (true) do
          begin
              write('choose option 1 to', Options);

              try
                readln(choice);
                if (choice>=1) and (choice <=Options) then
                    get_choice := choice
                else
                    write('invalid range');
              except
                 write('not a number');
          end;
      end;
8
  • are you going to tell use which line causes the error? Commented Jun 14, 2018 at 11:03
  • 2
    @Mawg, The System.Write can handle many types of arguments: Each output expression must be of a type Char, one of the Integer types (Byte, Shortint, Word, Longint, Cardinal), one of the floating-point types (Single, Real, Double, Extended, Currency), one of the string types (PChar, AnsiString, ShortString), a packed string, or one of the Boolean types (Boolean, Bool). Commented Jun 14, 2018 at 11:17
  • 2
    @Mawg: Write and Writeln are from the very old days. They don't need things like IntToStr or similar conversion routines. Both are "compiler magic" routines, i.e. the compiler knows them and generates different code sequences depending on the arguments. This also makes them accept multiple different arguments, which was otherwise not possible before array of const was introduced. FWIW, nowadays, such functions are called "intrinsics" by some. Commented Jun 14, 2018 at 15:09
  • 1
    Lolx! I have probably been needlessly calling calling IntToStr() with Write and()` for a few decades now :-) Commented Jun 14, 2018 at 15:12
  • 1
    @Mawg: you may want to read up on those routines: Write, Writeln, Read, Readln and see that some of them even have extra formatting options, like Write(I:8, J:8);which formats I and J right in their own "fields" of each 8 characters. Very useful for console programs that should quickly (and readably) output the results of a simple test. Commented Jun 14, 2018 at 15:15

2 Answers 2

5

As Sharam already stated, you missed the end of the try..except..end block. Nevertheless, I think I can enhance the answer a bit with some tips, to help avoiding this in you future.

  • keep your (begin .. end blocks on the same column. Same for try..except..end, try..finally..end and repeat..until blocks off course.
  • make use of the IDE's option to connect the begin and and blocks with lines. "structural highlighting. Check the attached screenshots.

Structural highlighting in action

Structural highlighting option

Sign up to request clarification or add additional context in comments.

7 Comments

This doesn't answer the question that was asked
I think otherwise. Helping out with a tip to avoid the same mistake in the future should be rewarded, not downplayed. Thanks for the comment anyway. I would rather like to put this in a comment in stead of an answer, but unfortunately my reputation does not allow that yet. (26/50)
Then you should earn the reputation first. I'm sorry, but that's site policy as has been discussed on meta many times. This answer would be fine if you answered the question as well. It doesn't matter if it's already been answered. The baseline, bare minimum, is to answer the question asked. That's not negotiable. If you want to add more then that's great. So you could address my criticism simply by an edit that addressed the question.
Thanks David, I got it now, and updated the answer. I'll see if I find some time to check the meta discussions.
Many thanks - I can see what David means but this is really useful for me and coincidentally was the exact question I asked previously and this is a much better answer so if you copy it over to there I will make this the favoured answer! Annoyingly my copy doesn't have Structural highlighting under the color tab for some reason. Cheers anyway
|
4

You missed an end:

              try
                readln(choice);
                if (choice>=1) and (choice <=Options) then
                    get_choice := choice
                else
                    write('invalid range');
              except
                 write('not a number');
              end;
         end
      end;

Try try..except block must have its own end.

1 Comment

Oh gosh, yes! Thanks very much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.