Federica Donald
Federica Donald

Reputation: 41

Pascal Coding Error?

I put in this code into Delphi but it came up with error

Operator not applicable to this operand type

I have no idea what that means and why it's coming up. I'm trying to only get the surname from an email with the format

[email protected]

where i is the first initial and yy is the year of joining the company. I know I can't just use midstr so I'm trying to use NOT commands to eliminate the rest of the email to leave the surname.

Here's the code:

uses
  System.SysUtils,
  strutils;

var email: string;

begin
writeln('input your email');
readln(email);
writeln('Your last name is ', (NOT leftstr(email,2)) AND (NOT rightstr(email,10)));
readln;
end.

Upvotes: 0

Views: 250

Answers (3)

David Heffernan
David Heffernan

Reputation: 613451

The and, or and not operators do not apply to strings. Your code is far from what you need.

But the biggest criticism I have here is the mixing of the string processing with your output code. I'd write a pure string processing helper.

function DecodeEmailAddressOK(
  const Address: string;
  out Initial: string;
  out Surname: string;
  out Year: Integer
): Boolean;
var
  AtPos: Integer;
begin
  AtPos := Pos('@', Address);
  if AtPos < 5 then
    Exit(False);
  Initial := Copy(Address, 1, 1);
  Surname := Copy(Address, 2, AtPos-3);
  Result := TryStrToInt(Copy(Address, AtPos-2, 2), Year);
end;

The error checking is very crude. I'm sure you'll be able to do better.

Upvotes: 2

Andreas Rejbrand
Andreas Rejbrand

Reputation: 109003

I am afraid you have completely misunderstood the meaning of the logical operators and, or, and not. These are not used with strings, but with booleans (logical values, which are either true or false).

not is a unary operator which returns true if the operand is false, and returns false if the operand is true. Thus, not true yields false, and not false yields true.

For example, if you have a boolean variable PasswordTooSimple (the typed password is too simple), you might want to set CanContinue := not PasswordTooSimple somewhere in your logic.

and is a binary operator on booleans, which returns true if and only if both of its operands are true. For example, CanContinue := ValidUser and not PasswordTooSimple.

or is a binary operator on booleans, which returns true if and only if at least one of its operands is true. For example, CanContinue := (PaymentComplete or NoncommersialVersion) and not PasswordTooSimple.

If PaymentComplete, NoncommersialVersion and PasswordTooSimple are false, true, and false, respectively, then, applying these rules, we find that PaymentComplete or NoncommersialVersion will evaluate to true, that not PasswordTooSimple will evaluate to true, and -- therefore -- the entire expression (PaymentComplete or NoncommersialVersion) and not PasswordTooSimple = true and true = true, so that CanContinue will be true. For example, you might have btnContinue.Enabled := CanContinue.

By the way, now you know what the compiler means by "Operator not applicable to this operand type". Indeed, the error message is spot on: the not operator is not applicable to string operands.

Upvotes: 1

Nick Hodges
Nick Hodges

Reputation: 17138

Sorry, but I confess I can't make head or tails of your code. You are definitely on the wrong track.

But, I can show you some pretty simple code that should get you going:

Please be warned, I'm writing this code off the top of my head. But don't worry, someone will be very quick to correct my mistakes. :-)

function ExtractNameFromEmail(aEmail: string): string;
var
  PositionOfAtSymbol: Integer;
begin
  if not aEmail.Contains('@') then
  begin
    WriteLn('I don''t think you passed an email address');
    Exit;
  end;

  PositionOfAtSymbol := Pos('@', aEmail);
  Result := Copy(aEmail, 1, PositionOfAtSymbol - 1);
end;

That will give you what you appear to be looking for. It takes a bit of counting (notice the need for the "- 1" in the final line) but that does what you want.

Upvotes: 1

Related Questions