Title: Problem with input
1Problem with input
2Consider the following program segment
var Xinteger begin ... readln(X) writeln(X
2) .
What if ABC is entered?
3Predefined Procedure VAL
Val ( S, N, E)
- A string
- To be converted to a number
- An integer indicating whether the conversion is
successful or not - 0 gt successful
- Egt0 gt there is error at position E
- An integer or a real
- Store the result of the conversion
4Example 1
Sstring N, E integer S123 val(S,N,E) wr
iteln(N) writeln(E)
Output 123 0
5Example 2
Sstring N, E integer S123AB val(S,N,E)
writeln(N) writeln(E)
Output 0 4
6Example 3
Sstring N, E integer S1.23 val(S,N,E) w
riteln(N) writeln(E)
Output 0 2
7Example 4
Sstring N, E real S123 val(S,N,E) write
ln(N) writeln(E)
Output 1.230000000E02 0
8Example 5
Sstring N, E real S1.23E01 val(S,N,E)
writeln(N) writeln(E)
Output 1.230000000E01 0
9Example 6
Sstring N, E real S1.23B01 val(S,N,E)
writeln(N) writeln(E)
Output 0.000000000E01 5
10Exercise 1
P.251 Q3
X Y a. 999 0 b. 6.660000000E02 0 c. 0
3 d. 9.876000000E01 0 e. 0 1 f. 0 6
11Exercise 2
Write a program that accept an input and
check whether it is an integer, a real number or
a string.
Sample 1 Please enter a piece of data123 It is
an integer. Sample 2 Please enter a piece of
data4.567 It is a real number. Sample 3 Please
enter a piece of dataPeter It is a string.
12Answer (Ex2) program E2 var Sstring E,
Iinteger Rreal begin write(Please enter a
piece of data) readln(S) val(S,I,E) if
E0 then writeln(It is an integer.) else
begin val(S,R,E) if E0 then writeln(It
is a real number.) else writeln(It is a
string.) end end.