Reputation: 139
I have got a code like this:
program Project1;
uses crt;
type alph=set of 'A'..'Z';
var mn:alph;
begin
clrscr;
if ('A' in mn) then writeln( 'Yes');
readln;
end.
It doesn't print anything and it throws some issues : project1.lpr(11,14) Warning: Variable "mn" does not seem to be initialized I don't understand why, is there something wrong?
Upvotes: 1
Views: 3300
Reputation: 125708
The declaration
type
Alpha = set of 'A'..'Z';
simply says that Alpha
is a type that is allowed to contain zero or more of the letters between A
and Z
inclusive. It does not mean that a variable of that type automatically contains every element of that set; it simply means that the variable will consist of a set of characters within that range.
var
mn: Alpha; // Uninitialized variable that can contain
// characters between 'A'..'Z'.
begin
mn := ['A'..'Z']; // Valid set of every member
mn := ['A', 'C', 'X']; // Valid set of three members
The compiler is correctly telling you that you have not assigned any value to mn
, and therefore you're using an uninitialized variable.
BTW, the standard convention in most Pascal dialects is to preface types with a T
to make it clear it's a type. So, with that in mind, here's a working version of the code you've posted with that correction included.
program Project1;
uses
crt;
type
TAlpha=set of 'A'..'Z';
var
mn: TAlpha;
begin
clrscr;
mn := ['A'..'Z'];
if ('A' in mn) then
Writeln('A is in mn');
{
My preference to the if statement above - prints true or false
depending on whether the character is in the set, so you get output
either way.
}
WriteLn('A in mn: ', ('A' in mn));
Readln;
end.
To address your additional question (from the comment below):
To check a string to see if all characters are numeric ('0'..'9'), you can do something like this:
function IsNumeric(const str: string): Boolean;
var
i: Integer;
begin
Result := True;
for i := 1 to Length(str) do
if not (str[i] in ['0'..'9']) then
Result := False;
end;
Upvotes: 4
Reputation: 17605
No, there is nothing wrong so far. However, as the warning says, nm
is not initialized. You will either run into undefined behaviour (I'm not sure whether this is actuall possible in Pascal) or the code snippet does nothing useful - you wish to check whether mn
contains 'A'
, but have not put anything into into mn
.
Upvotes: 0