Recent

Author Topic: Problem using Repeat Until  (Read 3447 times)

Pernicles

  • Newbie
  • Posts: 4
Problem using Repeat Until
« on: October 18, 2015, 11:46:37 pm »
Hi, My name is Fernando Pernesand i'm a beginner in pascal and i wroat a program with Repeat until and the program run but dont stop printing the same.

Here is the program:

Thanks.

-------------------------------

program Aluguer_1;

var Cfixo, CD5, CDmais5, Rendimento,
    Tipo, Caluguer: Real;
    Ndias: Real;

begin
   Write('Tipo de escavadora (1/2)?='); Read(Tipo);
   Write('Volume de Terras?='); Read(Rendimento);


   Ndias:= Rendimento / 8;


   If Ndias<=5 then

      Begin

       Repeat

        Cd5:=80;
        Cfixo:= 400;

        CAluguer:= Cfixo + Cd5*Ndias;
        Writeln('Custo de aluguer: ', Caluguer:6:2);

        until Tipo=1;



     end;

   If Ndias > 5  then

      Begin

       repeat

        Cd5:= 80;
        CDmais5:= 60;
        Cfixo:= 1000;

        CAluguer:= CFixo + Cd5 +CDmais5*(Ndias -5);
        Writeln('Custo de aluguer: ', Caluguer:6:2);

       until Tipo=1;



      end;

   If Ndias <=5 then

      Begin

       repeat

         Cd5:= 200;
         Cfixo:=1000;

         Caluguer:= Cfixo + cd5*Ndias;

         Writeln('Custo de aluguer: ', Caluguer:6:2);

         until Tipo=2;

      end;

   If Ndias>5 then

      Begin

      repeat

          Cd5:= 200;
          Cfixo:=1000;
          Cdmais5:= 100;

          Caluguer:= Cfixo + cd5 + cdmais5*(Ndias -5);

          Writeln('Custo de aluguer: ', Caluguer:6:2);

       until tipo =2;

       end;



end.

marcio2003

  • Jr. Member
  • **
  • Posts: 69
Re: Problem using Repeat Until
« Reply #1 on: October 19, 2015, 12:27:12 am »
Olá Pernicles, sua dúvida é básica de freepascal:
repeat ... until TIPO=1 significa que o laço repeat deve ser iniciado com TIPO<>1 e dentro do laço repeat a variável TIPO deverá em algum momento receber valor 1, senão o laço repeat entrará em loop infinito pois para parar o laço a variável TIPO terá que ser igual a 1.
Espero ter te ajudado?
Lazarus 2.0.10 Windows 10 64bits

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Problem using Repeat Until
« Reply #2 on: October 19, 2015, 02:50:40 am »
There are a couple of issues. Without compiling, I can see:

Tipo is of Real type, never use = to compare Real values. Floating point limitation might return 0.99999987234837453whatever to denote 1, thus = might fail. Round() up the Real value first.

All of your repeat-until loops are in the form of:
Code: Pascal  [Select][+][-]
  1. repeat
  2. until Tipo=...;
  3.  
but Tipo itself NEVER changes inside the loop, causing infinite loop.

marcio2003

  • Jr. Member
  • **
  • Posts: 69
Re: Problem using Repeat Until
« Reply #3 on: October 19, 2015, 03:25:26 am »
Olá novamente,
Analisando o seu código observei que você não precisa usar REPEAT.
Não estou certo do que este algorítimo faz mas sugiro que teste o código abaixo:

Code: Pascal  [Select][+][-]
  1. var
  2.   Cfixo, CD5, CDmais5, Rendimento, Tipo, Caluguer: Real;
  3.   Ndias: Real;
  4.  
  5. begin
  6.  
  7.   write('Tipo de escavadora (1/2)?= ');
  8.   readln( Tipo );
  9.  
  10.   write('Volume de Terras?= ');
  11.   readln( Rendimento );
  12.  
  13.   Ndias:= Rendimento / 8;
  14.  
  15.   writeln('Ndias = ', Ndias:6:2);
  16.  
  17.   if Tipo = 1 then
  18.     if Ndias <= 5 then
  19.     begin
  20.       Cd5:=80;
  21.       Cfixo:= 400;
  22.       CAluguer:= Cfixo + Cd5*Ndias;
  23.       Writeln('Custo do aluguel: ', Caluguer:6:2);
  24.     end
  25.     else { Ndias > 5 }
  26.     begin
  27.       Cd5:= 80;
  28.       CDmais5:= 60;
  29.       Cfixo:= 1000;
  30.       CAluguer:= CFixo + Cd5 +CDmais5*(Ndias -5);
  31.       Writeln('Custo do aluguel: ', Caluguer:6:2);
  32.     end;
  33.  
  34.  
  35.   if Tipo = 2 then
  36.     if Ndias <= 5 then
  37.     begin
  38.       Cd5:= 200;
  39.       Cfixo:=1000;
  40.       Caluguer:= Cfixo + cd5*Ndias;
  41.       Writeln('Custo do aluguel: ', Caluguer:6:2);
  42.     end
  43.     else { Ndias > 5 }
  44.     begin
  45.       Cd5:= 200;
  46.       Cfixo:=1000;
  47.       Cdmais5:= 100;
  48.       Caluguer:= Cfixo + cd5 + cdmais5*(Ndias -5);
  49.       Writeln('Custo do aluguel: ', Caluguer:6:2);
  50.     end;
  51.  
  52.   repeat until keypressed;
  53.  
  54. end.  

Este código poderia ser ainda melhorado porém para não dificultar o seu entendimento mantive algumas coisas como você as fez.
Bons estudos ...
Lazarus 2.0.10 Windows 10 64bits

Handoko

  • Hero Member
  • *****
  • Posts: 5158
  • My goal: build my own game engine using Lazarus
Re: Problem using Repeat Until
« Reply #4 on: October 19, 2015, 03:54:12 am »
Please write in English, so more people can join to help.

One possible way to use repeat - until:

Code: Pascal  [Select][+][-]
  1.   repeat
  2.     PreviousTipo := Tipo;
  3.     Read(Tipo); // set value for Tipo
  4.     ... // do something
  5.   until (Tipo = PreviousTipo); // comparison condition for Tipo

So the solution can be:

Code: Pascal  [Select][+][-]
  1. Tipo := 0;
  2. repeat
  3.   PreviousTipo := Tipo;
  4.   Write('Program will exit if tipo using the previous value');
  5.   Write('Provide a value for tipo (1/2) ?=');
  6.   Read(Tipo);
  7.   ...
  8.   ...
  9.   if (Tipo = 1) then begin
  10.     ...
  11.     ... // perform the calculation
  12.     ...
  13.   end;
  14.   if (Tipo = 2) then begin
  15.     ...
  16.     ... // perform the calculation
  17.     ...
  18.   end;
  19.   ...
  20.   ...
  21. until (Tipo = PreviousTipo);
« Last Edit: October 19, 2015, 04:00:17 am by Handoko »

Pernicles

  • Newbie
  • Posts: 4
Re: Problem using Repeat Until
« Reply #5 on: October 19, 2015, 08:56:30 am »
Hi,
I made with Repeat Until because the the teacher asked so.

I will made as you do.

Thanks

 

TinyPortal © 2005-2018