simple pascal algorithm

i want to add up all the decimal values of each individual character in a string. I get a compiler error (Error 39: Ordinal Expression Expected), i dont know how to solve it. Im trying this atm:
for count := 1 to max_length do
     begin
         sum := sum + ord(copy(stringvariable,count,1));
     end;

any help mucho appreciated :)
RevelationUKAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
ATAHACConnect With a Mentor Commented:
try this
for count := 1 to max_length do
     begin
         sum := sum + ord(stringvariable[count]);
     end;
0
 
xtremecrisCommented:
The problem was that you were trying to call the ORD function giving a string parameter. If you would have looked in the Help(just press CTRL+F1 with the cursor over the "ORD" name) you would see that the function accepts only chars as a parameter returning the ASCII order of that character.

Best regards,
Cris.
0
 
progexCommented:
Try this:

sum := sum + char(copy(stringvariable,count,1));
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
ZhaawZSoftware DeveloperCommented:
is max_length the same as length(stringvariable)?
0
 
xtremecrisCommented:
for count := 1 to length(stringvariable) do
     begin
         sum := sum + ord(stringvariable[count]);
     end;
0
 
AlievCommented:
Write full program. May be your types is wrong for this operation.
0
 
RevelationUKAuthor Commented:
its ok I worked it out, thanks anyway guys :)

sum := 0;
     max_length := length(password);
     for count := 1 to max_length do
     begin
          tempstring := copy(password,count,1);
          tempchar := tempstring[1];
          sum := sum + ord(tempstring[1]);
     end;
0
 
xtremecrisCommented:
That piece of code couldn't be more redundent even if you tried. Sorry for this remark but I couldn't help my self...

Why not use:  sum := sum + ord(password[count]); ?
Why is there a tempchar if you don't use it?

Best regards,
Cris.
0
 
ZhaawZSoftware DeveloperCommented:
Can be done also without using ord():

var
  str : string;
  arr : array of byte absolute str;
  n, sum : integer;
...
sum := 0;
for n := low(arr) to high(arr) do sum := sum + arr[n];
0
 
mlmccCommented:
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I will leave the following recommendation for this question in the Cleanup topic area:
   Accept - ATAHAC

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

mlmcc
Pascal PE
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.