I get the error Error: Operator is not overloaded on line 7. Do I have to do a another repeat and can’t use the and operator?
Function GetValidPlayerName : String;
Var
PlayerName : String;
Begin
Repeat
Readln(PlayerName);
If PlayerName = '' And Length(PlayerName) > 10
Then Write('That was not a valid name. Please try again: ');
Until PlayerName <> '';
GetValidPlayerName := PlayerName;
End;
First, you need to write
The parentheses are required.
Secondly, this will always evaluate to
false, because there is no string that is both empty and has length 11 or more. Indeed, a string is empty if and only if its length is zero, so basically you say “if the length is zero and the length is 11 or more, then…”.Most likely you wish instead to use a disjunction, that is, to use
orinstead ofand:This will display the error message if the name is empty or if it is too long.
In addition, the loop will exit even if the name is invalid, because if
PlayerNameis equal toThisIsATooLongNamethen indeedPlayerName <> ''.What you need is something like
or