Foros aprenderaprogramar.com

Aprender a programar => C, C++, C#, Java, Visual Basic, HTML, PHP, CSS, Javascript, Ajax, Joomla, MySql y más => Mensaje iniciado por: zadok en 03 de Marzo 2015, 18:26

Título: Ejemplo bucle while en Pascal, programa que no termine hasta cumplirse condición
Publicado por: zadok en 03 de Marzo 2015, 18:26
Hola otra vez, como no cuanto más quiere hacer uno más dudas y más problemas surgen, el caso es que cuando ejecuto el programa, al final se acaba cerrando.

¿Cómo hago para que se reinicie?

Ejemplo

Código: [Seleccionar]
writeln;
   writeln;
   writeln('Pulsa ENTER para salir');
   readln
end.

Como hago para que llegados a ese punto, el programa de la opción de salir o reiniciarlo, he probado con:

Código: [Seleccionar]
program portas;

USES Crt;

var
numporta: string;
dec: char;

Procedure inicio ;
begin

.....
.....
     writeln;
     writeln;
     writeln ('Continuar? (S/N)') ;
     readln (dec);
     if (dec = ('S'))
     then begin inicio;
     else
end.
Título: Re:Reiniciar el programa
Publicado por: javi in the sky en 03 de Marzo 2015, 23:28
Hola usa un bucle while para controlar cuándo el usario ha respondido que quiere continuar.

Ejemplo

Código: [Seleccionar]
PROGRAM CUADRADOS;
VAR
n, c : INTEGER;
BEGIN
WRITELN('Introduzca un numero entero: ');
READLN(n);
WHILE (n<>0) DO
BEGIN
c := n * n;
WRITELN('El cuadrado de ', n, ' es ', c);
WRITELN('Introduzca un n umero entero: ');
READLN(n);
END;
WRITELN('PULSA ENTER PARA FINALIZAR');
READLN;
END.

Saludos
Título: Re:Ejemplo bucle while en Pascal, programa que no termine hasta cumplirse condición
Publicado por: zadok en 09 de Marzo 2015, 09:24
No consigo hacerlo y lo he intentado de muchas maneras, como me dijiste y de otras muchas que vi por internet, en algo fallo... pongo el ejemplo de mi programa que será más práctico que hacer suposiciones jeje

Código: [Seleccionar]
program portas;
USES Crt;

var
numporta: string;

begin
writeln ('Escribe el numero del porta, matricula o nombre del conductor (mayuscula)...');
writeln ('');
readln(numporta);


//Portavehiculos 001
if (numporta = ('001')) or (numporta = ('0000XXX')) or (numporta = ('NOMBRE1'))
   then
   begin
        writeln('NOMBRE1');
        writeln('Portavehiculos 001');
        writeln('Matricula: 0000-XXX');
        writeln('EXT:00001');
        writeln('Movil: 666666666');
        writeln ('=============================');
   end

//Portavehiculos 002
else if (numporta = ('002')) or (numporta = ('0000ZZZ')) or (numporta = ('NOMBRE2'))
   then
   begin
        writeln('NOMBRE2');
        writeln('Portavehiculos 002');
        writeln('Matricula: 0000-ZZZ');
        writeln('EXT:00002');
        writeln('Movil: 666666667');
        writeln ('=============================');
   end


//Portavehiculos 003
else if (numporta = ('003')) or (numporta = ('0000VVV')) or (numporta = ('NOMBRE3'))
   then
   begin
        writeln('NOMBRE3');
        writeln('Portavehiculos 003');
        writeln('Matricula: 0000-VVV');
        writeln('EXT:00003');
        writeln('Movil: 666666668');
        writeln ('=============================');
   end

writeln;
   writeln;
   writeln('Pulsa ENTER para salir');
   readln
end.


En ese caso, como hago que al llegar al final, si deseamos volver a realizar una búsqueda, reinicie todo?


Gracias!
Título: Re:Ejemplo bucle while en Pascal, programa que no termine hasta cumplirse condición
Publicado por: javi in the sky en 09 de Marzo 2015, 11:15
Hola la idea es introducir un bucle while. Si no te sale prueba a hacer pequeños programas probando el while antes del programa en sí:

Código: [Seleccionar]
program portas;
USES Crt;

var
numporta: string;
quiereReptir: string;

begin
quiereRepetir:='s';
WHILE (quiereRepetir=('s')) DO
writeln ('Escribe el numero del porta, matricula o nombre del conductor (mayuscula)...');
writeln ('');
readln(numporta);


//Portavehiculos 001
if (numporta = ('001')) or (numporta = ('0000XXX')) or (numporta = ('NOMBRE1'))
   then
   begin
        writeln('NOMBRE1');
        writeln('Portavehiculos 001');
        writeln('Matricula: 0000-XXX');
        writeln('EXT:00001');
        writeln('Movil: 666666666');
        writeln ('=============================');
   end

//Portavehiculos 002
else if (numporta = ('002')) or (numporta = ('0000ZZZ')) or (numporta = ('NOMBRE2'))
   then
   begin
        writeln('NOMBRE2');
        writeln('Portavehiculos 002');
        writeln('Matricula: 0000-ZZZ');
        writeln('EXT:00002');
        writeln('Movil: 666666667');
        writeln ('=============================');
   end


//Portavehiculos 003
else if (numporta = ('003')) or (numporta = ('0000VVV')) or (numporta = ('NOMBRE3'))
   then
   begin
        writeln('NOMBRE3');
        writeln('Portavehiculos 003');
        writeln('Matricula: 0000-VVV');
        writeln('EXT:00003');
        writeln('Movil: 666666668');
        writeln ('=============================');
   end

writeln;
writeln ('¿Quiere repetir (s/n)');
writeln ('');
readln(quiereRepetir);
END;
end.
Título: Re:Ejemplo bucle while en Pascal, programa que no termine hasta cumplirse condición
Publicado por: zadok en 10 de Marzo 2015, 09:29
Llego al mismo punto que otras veces, al introducir el buble while..do me repite la siguiente linea infinidad de veces, pero no ejecuta el resto del código...


Edit: Solucionado, muchas gracias por tu ayuda, al final resultó que me faltaba un begin...
Título: Re:Ejemplo bucle while en Pascal, programa que no termine hasta cumplirse condición
Publicado por: javi in the sky en 10 de Marzo 2015, 10:39
¡Pega el código con la solución! ¡Puede ayudar a otras personas! Saludos
Título: Re:Ejemplo bucle while en Pascal, programa que no termine hasta cumplirse condición
Publicado por: zadok en 10 de Marzo 2015, 10:44
Aquí va:

Código: [Seleccionar]
program portas;
USES Crt;

var
rbusqueda: char;
numporta: string;

begin

rbusqueda := 'S';

WHILE (rbusqueda = ('S')) DO

begin

writeln ('Escribe el numero del porta, matricula o nombre del conductor (mayuscula)...');
writeln ('');
readln(numporta);


//Portavehiculos 001
if (numporta = ('001')) or (numporta = ('0000XXX')) or (numporta = ('NOMBRE1'))
   then
   begin
        writeln('NOMBRE1');
        writeln('Portavehiculos 001');
        writeln('Matricula: 0000-XXX');
        writeln('EXT:00001');
        writeln('Movil: 666666666');
        writeln ('=============================');
   end

//Portavehiculos 002
else if (numporta = ('002')) or (numporta = ('0000ZZZ')) or (numporta = ('NOMBRE2'))
   then
   begin
        writeln('NOMBRE2');
        writeln('Portavehiculos 002');
        writeln('Matricula: 0000-ZZZ');
        writeln('EXT:00002');
        writeln('Movil: 666666667');
        writeln ('=============================');
   end


//Portavehiculos 003
else if (numporta = ('003')) or (numporta = ('0000VVV')) or (numporta = ('NOMBRE3'))
   then
   begin
        writeln('NOMBRE3');
        writeln('Portavehiculos 003');
        writeln('Matricula: 0000-VVV');
        writeln('EXT:00003');
        writeln('Movil: 666666668');
        writeln ('=============================');
   end

else writeln('No es un numero de porta valido');

     writeln;
     writeln ('¿Quiere realizar otra busqueda? (S/N)');
     writeln ('');
     readln(rbusqueda);
     end;



end.




Justo despues de while...do, va un begin, sino lo que hace es repetir la siguiente linea infinidad de veces ;)
Título: Re:Ejemplo bucle while en Pascal, programa que no termine hasta cumplirse condición
Publicado por: javi in the sky en 10 de Marzo 2015, 10:48
Gracias!
Título: Re:Ejemplo bucle while en Pascal, programa que no termine hasta cumplirse condición
Publicado por: zadok en 10 de Marzo 2015, 10:48
A tí por la ayuda  ;)