Solved

simple pascal algorithm

Posted on 2004-03-25
11
287 Views
Last Modified: 2012-06-27
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 :)
0
Comment
Question by:RevelationUK
11 Comments
 
LVL 4

Accepted Solution

by:
ATAHAC earned 50 total points
Comment Utility
try this
for count := 1 to max_length do
     begin
         sum := sum + ord(stringvariable[count]);
     end;
0
 
LVL 1

Expert Comment

by:xtremecris
Comment Utility
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
 
LVL 1

Expert Comment

by:progex
Comment Utility
Try this:

sum := sum + char(copy(stringvariable,count,1));
0
 
LVL 11

Expert Comment

by:ZhaawZ
Comment Utility
is max_length the same as length(stringvariable)?
0
 
LVL 1

Expert Comment

by:xtremecris
Comment Utility
for count := 1 to length(stringvariable) do
     begin
         sum := sum + ord(stringvariable[count]);
     end;
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 3

Expert Comment

by:Aliev
Comment Utility
Write full program. May be your types is wrong for this operation.
0
 

Author Comment

by:RevelationUK
Comment Utility
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
 
LVL 1

Expert Comment

by:xtremecris
Comment Utility
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
 
LVL 11

Expert Comment

by:ZhaawZ
Comment Utility
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
 
LVL 100

Expert Comment

by:mlmcc
Comment Utility
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

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

HOW TO: Upload an ISO image to a VMware datastore for use with VMware vSphere Hypervisor 6.5 (ESXi 6.5) using the vSphere Host Client, and checking its MD5 checksum signature is correct.  It's a good idea to compare checksums, because many installat…
Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now