How to find values in pascal

Asked

Viewed 720 times

1

I’m setting up a program here at 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 your age and weight. My problem is just this, how do I find this 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.

3 answers

1

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

Imagine that you are old is 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 id(name or number) and position 2 receives another array containing Data.

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

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

0

In the name search, take the name typed put in a variable then create a for to list all elements, within it enter a if which checks if name is equal to the current entry in the record. Note that a counter will be required

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;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.