Problem with input - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

Problem with input

Description:

writeln(X 2); What if ABC' is entered? Predefined Procedure VAL. Val ( S, N, E) A string ... E=0 then. writeln( It is a real number.) else writeln( It is a ... – PowerPoint PPT presentation

Number of Views:18
Avg rating:3.0/5.0
Slides: 13
Provided by: pete1158
Category:
Tags: input | problem | writeln

less

Transcript and Presenter's Notes

Title: Problem with input


1
Problem with input
  • Type mismatch

2
Consider the following program segment
var Xinteger begin ... readln(X) writeln(X
2) .
What if ABC is entered?
3
Predefined 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

4
Example 1
Sstring N, E integer S123 val(S,N,E) wr
iteln(N) writeln(E)
Output 123 0
5
Example 2
Sstring N, E integer S123AB val(S,N,E)
writeln(N) writeln(E)
Output 0 4
6
Example 3
Sstring N, E integer S1.23 val(S,N,E) w
riteln(N) writeln(E)
Output 0 2
7
Example 4
Sstring N, E real S123 val(S,N,E) write
ln(N) writeln(E)
Output 1.230000000E02 0
8
Example 5
Sstring N, E real S1.23E01 val(S,N,E)
writeln(N) writeln(E)
Output 1.230000000E01 0
9
Example 6
Sstring N, E real S1.23B01 val(S,N,E)
writeln(N) writeln(E)
Output 0.000000000E01 5
10
Exercise 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
11
Exercise 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.
12
Answer (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.
Write a Comment
User Comments (0)
About PowerShow.com