How to find values in pascal

1

I'm setting up a program here in Pascal, where I have a record with 3 positions, each with name, age and weight. I also have a procedure called Query, which will allow me to search for a registered name, and return their age and weight. My problem is precisely this, how do I get that name? Do you have to use for? Do you have to create a new array? Give me a light: D

If you want to see the program, follow below:

Program Pzim ;

var opc, num: integer;
    esc: char;
    cad:array[1..3] of record
        nome: string[30];
        idade: integer;
        peso: real;
    end;

procedure menu;
begin
    writeln;
    writeln('1. Cadastro');
    writeln('2. Consulta');
    writeln('3. Exclusao');
    writeln('4. Sair');
    writeln;
    write('Opcao: ');
    readln(opc);
    writeln;
end;

procedure cadastro;
begin
     while (esc <> 'n') do
    begin
        writeln('Digite o seu nome:');
        readln(cad[num].nome);
        writeln('Digite a sua idade:');
        readln(cad[num].idade);
        writeln('Digite o seu peso:');
        readln(cad[num].peso);
        writeln('Cadastro concluído. Gostaria de realizar outro? (s: sim/n: nao)');
        readln(esc);
        num:=num+1;
    end;

    if (num>3) then
    begin
        writeln('Você só pode fazer 3 cadastros!');
        menu;
    end;

    menu;
end;

procedure consulta;
begin
    while (esc<>'n') do
    begin
        writeln('Digite o nome do usuário cadastrado:')
        readln();


Begin
num:=1;
menu;

if opc=1 then
    begin
    cadastro;
    end;

End.
    
asked by anonymous 30.04.2014 / 00:49

3 answers

1

You need to create an array to retrieve these values from the array key.

Imagine that you have age and weight. So basically you need to give an "id" to your array.

Example.

variável Pessoa = array[2];
variável DadosPessoa = array[2];

Position 1 of the Person array receives the person's id (name or number) and position 2 receives another array containing Person Data.

So by calling position 1 of the array you can restore your age and weight.

I gave a simple example in pseudocode but I think you got the idea.

    
30.04.2014 / 01:06
0

In search by name, get the name you typed put in a variable then create a for to list all elements, enter it with a if that checks if name is equal to the current record item. Remember that you will need a counter

    
30.04.2014 / 03:46
0
procedure consulta;
var nome_consulta:string; i:Integer;
begin
    while (esc<>'n') do
    begin
        writeln('Digite o nome do usuário cadastrado:')
        readln(nome_consulta);
        for i:=1 to num do
        begin
           if trim(cad[num].nome) = trim(nome_consulta) then
           begin
               writeln(cad[num].nome);
               writeln(cad[num].idade);
               writeln(cad[num].peso);
           end;
         end;
    end;
end;
    
27.02.2015 / 03:19