When you create a variable within a method / function / procedure, this variable can only be accessed within the scope itself, so you can not access the variable of function 1 being in function 2 unless it is a return or parameter passed by reference.
Another way to do what you need to do is to use a global variable, I do not think it's the best way, but it caters and it's also easier to understand for those starting out.
To create a global variable, we also use the var
section, but just below the uses
section:
uses
SysUtils;
var
jogadores: ?????
But here comes a question: What should be the type of the variable
jogadores
? Since for soccer we will have 11, basketball 5 and volleyball 6.
The trick is: we do not need to set the size of a array
in the declaration, we can define it in the future. To do this, simply define it as a array
of string
:
uses
SysUtils;
var
jogadores: Array of String;
Now that we have the global variable to store the players, we will create the methods responsible for setting the size of the array
and requesting the names to be stored.
But how do we set the size of array
during the execution of
program?
Simple, we'll use the SetLength function. This function serves both to set the size of a string
and the size of a array
. In our case, we will define the size of a array
, the first parameter is the variable that will be resized and the second is the new size of the variable:
uses
SysUtils;
var
jogadores: Array of String;
procedure futebol;
var
i: integer;
begin
SetLength(jogadores, 11);
for i := 1 to 11 do
begin
writeln('Digite o nome do jogador ', i);
readln(jogadores[i - 1]);
end;
end;
procedure basquete;
var
i: integer;
begin
SetLength(jogadores, 5);
for i := 1 to 5 do
begin
writeln('Digite o nome do jogador ', i);
readln(jogadores[i - 1]);
end;
end;
procedure volei;
var
i: integer;
begin
SetLength(jogadores, 6);
for i := 1 to 6 do
begin
writeln('Digite o nome do jogador ', i);
readln(jogadores[i - 1]);
end;
end;
Note that this way the first item of array
will always be the
position 0
, so when we are assigning we need to subtract 1
of the number the user is viewing.
Now just implement the process you've already done, just changing to jogadores
the places that are like jogador
:
uses
SysUtils;
var
jogadores: Array of String;
procedure futebol;
var
i: integer;
begin
SetLength(jogadores, 11);
for i := 1 to 11 do
begin
writeln('Digite o nome do jogador ', i);
readln(jogadores[i - 1]);
end;
end;
procedure basquete;
var
i: integer;
begin
SetLength(jogadores, 5);
for i := 1 to 5 do
begin
writeln('Digite o nome do jogador ', i);
readln(jogadores[i - 1]);
end;
end;
procedure volei;
var
i: integer;
begin
SetLength(jogadores, 6);
for i := 1 to 6 do
begin
writeln('Digite o nome do jogador ', i);
readln(jogadores[i - 1]);
end;
end;
var
esporte, verificar: char;
i: integer;
begin
writeln('Digite o esporte que voce joga([f - futebol][b - basquete][v - volei])');
readln(esporte);
if esporte = 'f' then
begin
futebol;
end
else if esporte = 'b' then
begin
basquete;
end
else if esporte = 'v' then
begin
volei;
end;
writeln('Deseja verificar o nome dos jogadores(s/n)');
readln(verificar);
while verificar = 's' do
begin
writeln('Digite o numero do jogador');
readln(i);
writeln('Nome do jogador ', i, ': ', jogadores[i - 1]);
writeln('Deseja verificar outro jogador(s/n)');
readln(verificar);
end;
readln;
end.