I am currently learning Pascal at school an I'm thought i was getting pretty efficient until i was set work on the repeat until loop. I was working on a program that squares then cubes a number until a rouge value of -9999.99 but it won't!

(* Author: Tony Diec
   Date: Tuesday, 23rd September 2008
   Description: Squares And Cubes Numbers.
*)


program SquareCube;
   

var
   Num1, Square, Cube : real;
   
procedure Obtain;
         begin
           Square := Num1 * Num1;
            writeln (Num1,' Squared = ',Square);
            Cube := Square * Num1;
            writeln (Num1,' Cubed = ',Cube);
            writeln ('Input The Number You Would Like To Square And Cube');
            writeln ('WARNING! Entering -9999.99 Will End Program');
            readln (Num1);
end;
procedure ObtainNum;
   begin
      writeln ('Input The Number You Would Like To Square And Cube');
      writeln ('WARNING! Entering -9999.99 Will End Program');
      readln (Num1);
         repeat
            Obtain
         until Num1 = -9999.99;
   end;
   

   
   begin
      
      ObtainNum;
      readln;
      
   end.

Dani AI

Generated

The repeat..until structure itself is fine; the typical causes for a loop that "won't stop" here are (a) testing a floating-point value for exact equality and (b) input/locale differences for the decimal separator. pointed to the fragility of exact equality checks, and flagged locale/decimal issues. A more robust pattern is to treat the sentinel as text, check it before converting, then parse the numeric input.

A practical, portable pattern: read each line into a string, trim and compare the raw string to the sentinel, then convert the string to a float and compute. This avoids floating-point equality problems and lets the program detect the sentinel even if parsing would round or change the value. Example (Delphi-style Pascal):

uses
  SysUtils;

var
  s: string;
  v: Double;
begin
  DecimalSeparator := '.'; // adjust for local conventions if needed
  writeln('Enter numbers; type -9999.99 to quit.');
  while True do
  begin
    readln(s);
    s := Trim(s);
    if s = '-9999.99' then
      Break;
    if TryStrToFloat(s, v) then
    begin
      writeln(s, ' squared = ', FormatFloat('0.#####', v * v));
      writeln(s, ' cubed   = ', FormatFloat('0.#####', v * v * v));
    end
    else
      writeln('Invalid input: ', s);
  end;
end.

Notes and fallbacks: if the compiler lacks TryStrToFloat, use Val and check the error code. If the environment uses a comma as the decimal separator, either set DecimalSeparator appropriately or accept both by replacing ',' with '.' in the input string before parsing. For quick debugging, log the raw input string and the parsed numeric value to confirm what the program is actually seeing.

you didnt change the numl and expect your code to stop looping when numl reach -9999.99.

maybe you should consider if else statement...not looping:)

or substract the numl...so the numl reach the number...

anyway,its very risky to loop until an amount of number with decimal in it and "=" operation.use > or < instead:)

First, an if would be nicer, but then he should use break, and that's forbidden in school.
And then he doesn't have to increase Numl, because it's reobtained inside the loop. You're supposed to input that value exactly, so the = is also fine here. You can't see this, because it's a proc oriented code with global variables. This is exactly why OO is cool.
And just for help for those who want to test it on delphi: use .dpr extension, and add the {$Apptype console} directive.
Oh, and the bug at last: I don't see where are you from in your profile, but I'm from Hungary, and I had to input "-9999,99" instead of "9999.99". Try hitting the decimal on your numpad, it's always the right one. But more importantly it works for me as it is, so try changing that lot of 9s to a 0, that will work the same way everywhere.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.