#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# How to calculate sum of numeric character within text line

Member Posts: 4
I have a problem converting a numeric characters to numeric numbers and calculate the "sum" of those numbers; I have to use Val Function/procedure to determine if the input is a numeric character or not, the Val procedure format is: Val(S:string,V:lnteger,Code:integer)
and have to copy the numeric character to another string then extracting the numbers usin Val function for the calculation of sum as an example:
var s: string;
if the input: '25*-2g &&-+ad dd15wd'
the output should be: 38
if the input: '25 -10' the output should be 15
** I will be appreciated ur effort to help me find out how to solve this problem.

• Member Posts: 6,349
: I have a problem converting a numeric characters to numeric numbers and calculate the "sum" of those numbers; I have to use Val Function/procedure to determine if the input is a numeric character or not, the Val procedure format is: Val(S:string,V:lnteger,Code:integer)
: and have to copy the numeric character to another string then extracting the numbers usin Val function for the calculation of sum as an example:
: var s: string;
: if the input: '25*-2g &&-+ad dd15wd'
: the output should be: 38
: if the input: '25 -10' the output should be 15
: ** I will be appreciated ur effort to help me find out how to solve this problem.
:
I would loop through the string and check if each char is a "-" or a number (0..9). If it is, then start to create a new string holding that character. Copy all following numbers in that string. And finally convert that string to a number and add it to the sum.
• Member Posts: 4
: : I have a problem converting a numeric characters to numeric numbers and calculate the "sum" of those numbers; I have to use Val Function/procedure to determine if the input is a numeric character or not, the Val procedure format is: Val(S:string,V:lnteger,Code:integer)
: : and have to copy the numeric character to another string then extracting the numbers usin Val function for the calculation of sum as an example:
: : var s: string;
: : if the input: '25*-2g &&-+ad dd15wd'
: : the output should be: 38
: : if the input: '25 -10' the output should be 15
: : ** I will be appreciated ur effort to help me find out how to solve this problem.
: :
: I would loop through the string and check if each char is a "-" or a number (0..9). If it is, then start to create a new string holding that character. Copy all following numbers in that string. And finally convert that string to a number and add it to the sum.
:

Actually I might present the problem incorrectly, so; let me present it in another way as following:

Write a Pascal program that reads a line of text and prints a sum for all of the integers contained in the input line.
The line of text will includes extraneous characters, as well as integers.

25 -10

it will output:

15
38

* To ignore the extraneous characters you will need to read each line of text
into a string and then extract the integers from the string.

* You should use Val function to determine if a given string character is the
first character in a valid integer.

* If the value of the code parameter is one, then the character is non numeric.
If the value of the code parameter is greater than one, then you are looking
at the first character in a valid integer and the value of code indicates the
the position of the first non-numeric character.

* To extract the value of the integer you must copy the numeric characters to
another string and then use the Val function to extract the new string's numeric
value. If the value of the code is zero, then a valid integer has been read.

• Member Posts: 6,349
: : : I have a problem converting a numeric characters to numeric numbers and calculate the "sum" of those numbers; I have to use Val Function/procedure to determine if the input is a numeric character or not, the Val procedure format is: Val(S:string,V:lnteger,Code:integer)
: : : and have to copy the numeric character to another string then extracting the numbers usin Val function for the calculation of sum as an example:
: : : var s: string;
: : : if the input: '25*-2g &&-+ad dd15wd'
: : : the output should be: 38
: : : if the input: '25 -10' the output should be 15
: : : ** I will be appreciated ur effort to help me find out how to solve this problem.
: : :
: : I would loop through the string and check if each char is a "-" or a number (0..9). If it is, then start to create a new string holding that character. Copy all following numbers in that string. And finally convert that string to a number and add it to the sum.
: :
:
: Actually I might present the problem incorrectly, so; let me present it in another way as following:
:
: Write a Pascal program that reads a line of text and prints a sum for all of the integers contained in the input line.
: The line of text will includes extraneous characters, as well as integers.
:
: 25 -10
:
: it will output:
:
: 15
: 38
:
: * To ignore the extraneous characters you will need to read each line of text
: into a string and then extract the integers from the string.
:
: * You should use Val function to determine if a given string character is the
: first character in a valid integer.
:
: * If the value of the code parameter is one, then the character is non numeric.
: If the value of the code parameter is greater than one, then you are looking
: at the first character in a valid integer and the value of code indicates the
: the position of the first non-numeric character.
:
: * To extract the value of the integer you must copy the numeric characters to
: another string and then use the Val function to extract the new string's numeric
: value. If the value of the code is zero, then a valid integer has been read.
:
:
:
That's even easier. Just loop through all the characters (using a repeat until loop) and inside the loop pass the counter-th character to the Val() procedure. The create a case-of of the returned code to differentiate between the following codes:

0: add the returned value to the sum and increase counter by 1
1: increase counter by 1
else: use Copy() to pass the substring to Val(), add the returned value to the sum, and increase counter by the case value

This is nearly the entire code written out in english. There still needs one or two if-then statements to catch possible invalid values, but the rest is there.