0

I'm playing around with the comp 1 pre release material, but I can't seem to asnwer this properly in Pascal compiling in delphi...

  • This question refers to the function RollBowlDie.
  • This function has no validation of the input value.

  • What you have to do.... Make a copy of the Skeleton Program. Add additional statements to the function RollBowlDie so that it ensures that the input BowlDieResult is in the range 1 to 6 before allowing the game to continue and outputs an error message if not.

  • Add further statements to the function RollBowlDie so that it also checks that the input BowlDieResult does not cause the program to crash if it is the wrong data type and instead outputs an error message when this is detected.

  • Test the amended function to show that it does not allow inputs that are out of range.

  • Test the amended function to show that it does not crash if an incorrect data type is added.

This is the RollBowlDie function.

Function RollBowlDie(VirtualDiceGame : Boolean) : Integer;
  Var
    BowlDieResult : Integer;
  Begin
    If VirtualDiceGame
      Then BowlDieResult := Random(6) + 1
      Else
        Begin
          Writeln('Please roll the bowling die and then enter your result.');
          Writeln;
          Writeln('Enter 1 if the result is a 1');
          Writeln('Enter 2 if the result is a 2');
          Writeln('Enter 3 if the result is a 4');
          Writeln('Enter 4 if the result is a 6');
          Writeln('Enter 5 if the result is a 0');
          Writeln('Enter 6 if the result is OUT');
          Writeln;
          Write('Result: ');
          Readln(BowlDieResult);
          Writeln;
        End;
    RollBowlDie := BowlDieResult;
  End;

I have tried putting this in, but the message error message didnt come up, and the program crashed when trying in a letter.

Function RollBowlDie(VirtualDiceGame : Boolean) : Integer;
  Var
    BowlDieResult : Integer;
  Begin
    If VirtualDiceGame
      Then BowlDieResult := Random(6) + 1
      Else
        Begin
          Repeat
            Writeln('Please roll the bowling die and then enter your result.');
            Writeln;
            Writeln('Enter 1 if the result is a 1');
            Writeln('Enter 2 if the result is a 2');
            Writeln('Enter 3 if the result is a 4');
            Writeln('Enter 4 if the result is a 6');
            Writeln('Enter 5 if the result is a 0');
            Writeln('Enter 6 if the result is OUT');
            Writeln;
            Write('Result: ');
            Try
              Readln(BowlDieResult)
            Except
              Writeln('Not a valid number')
            End;
            Writeln;
          Until (BowlDieResult >= 1) and (BowlDieResult <= 6);
        End;
    RollBowlDie := BowlDieResult;
  End;

I's not sure how to solve the question, any help would be appreciated greatly!

1
  • This is basically an expanded duplicate of this question, and the answer to that one should solve many of your issues here. Commented May 20, 2011 at 21:26

2 Answers 2

2

Probably you need to read a string or a char-typed variable instead of an integer one, and then convert the string/char to integer in an controlled way.

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

2 Comments

e.g. using TryStrToInt. if TryStrToInt(ch, Answer) and (Answer <= 6) and (Answer >= 1) then ... else writeln('Invalid input');
This is was I used in the end, read as a char, convert using val function.
1

Of course it crashes if you type in a letter ... you're specifically asking to read an integer, and a letter isn't an integer.

Before commenting more on that issue, why not just say:

writeln ('Please enter the number rolled, or 0 if it is an OUT: ');

rather than having 6 writelns? Also, what if a 3 or 5 was rolled (you're only giving (somewhat odd) directions for the values 1, 2, 4, 6, and 0 ... leaving out 3 and 5. Keep in mind that you can't get a 0 from "BowlDieResult := Random(6) + 1".

How do you intend the user to indicate "stop" if you're looping until you have a value between 1 and 6?

Back to the "how do I read a letter" question...

Use a variable of type "char" (or "packed array of char") to read arbitrary text ... then, compare the character read to '1', '2', ..., '6', or (say) 'Q' (for Quit).

E.g.:

var 
   answer : char;
   attempts : integer;      {prevent infinite loop}
   done : boolean;

attempts := 0;      
saw_quit := false;

done := false;          {loop until we get a good number or a QUIT command}
                        {Or until 9 attempts have been made to enter a #. }

while not done do
   begin
   writeln ('Please enter a number (1..6) or Q to quit: ');
   readln (answer);

   if answer in ['1'..'6'] then
      begin          {we got a number in range 1..6...}
      BowlDieResult := ord (answer) - ord ('0');     {convert char to int}
      done := true;
      end

   else if answer in ['Q', 'q'] then         {upper or lower case :) }
      begin
      saw_quit := true;
      done := true;
      end

   else
      begin
      writeln ('Sorry, that is not a number from 1 to 6 or a "Q"!');

      attempts := attempts + 1;
      if attempts > 9 then
         begin
         writeln ('Sorry, too many mistakes ... assuming QUIT');
         saw_quit := true;
         done := true;
         end;
      end;
   end;        {while not done}

Note: the above not tested for compilation ... I NEVER use Pascal's built-in I/O, for performance and reliability reasons, so I'm rusty with it.

Note: see http://www.allegro.com/papers/htpp.html for some philosophy of Pascal programming.

Stan

1 Comment

Thanks for the answer, but i'm sorry i didn't specify i didnt want the whole program re-arranged, this would take too long in the exam. I've looked over your answer none the less and it provided some helpful tips. Along with the discovery of the val function I have been able to solve it. Thanks!

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.