get input number

advertisement
Programming Examples to
Accompany Structure Topic
Please use speaker notes for
additional information!
Start
Get
input
number
not
end of user
input
Y
Calculate the double
of the input by
multiplying by 2
Display/print
answer
N
Stop
Pseudocode:
start
get input number
while not end of user input
calculate the answer (multiply input by 2)
display/print the answer
get input number
end loop
stop
Get
input
number
COBOL
This shows the program written
from the logic in the flowchart
and pseudocode on the previous
slide. It also shows the output
that was generated when the
program was run.
COBOL
This shows the definition of
the variables I am going to
use in the program.
INPUT-NUM is where I will
have the user key in a number
to be doubled.
DOUBLE-NUM will hold the
result of doubling the number.
ANS-WS is where the enter
key the user presses when
they want to move on to the
next number is stored.
COBOL
I am using the DISPLAY AND ACCEPT
commands to take in user input. The
DISPLAY asks the user to key in information
and the ACCEPT takes the information and
stores it in the place of that name.
DISPLAY “ENTER INPUT NUMBER”.
This command prompts the user to enter a
number.
ACCEPT INPUT-NUM.
This command takes the data the user enters
(note the data cannot be more than two
characters) and stores it in INPUT-NUM.
Note that when the user entered 123456,
the computer took only the 12 and stored
it as INPUT-NUM. This means that
DOUBLE-NUM will be 024. It is 024
because DOUBLE-NUM has a PIC of
999 and can therefore hold three
numbers.
COBOL
PERFORM UNTIL INPUT-NUM=99 means
that processing will stop when the user enters
99.
While the user enters other numbers the
processing between the PERFORM and the
END-PERFORM will be executed. Note that
one of the things I added to the pseudocode
was the end loop.
Pseudocode:
start
get input number
while not end of user input
calculate the answer (multiply input by 2)
display/print the answer
get input number
end loop
stop
The processing that is done is to use the
COMPUTE statement to do the math and then
DISPLAY the original number and the double
number. The ACCEPT ANS-WS waits for the
user to press the enter key or take some other
keyboard action. Then the processing takes in
a new number.
COBOL
IDENTIFICATION DIVISION.
PROGRAM-ID. DOUBLE.
DATA DIVISION.
Pseudocode:
FILE SECTION.
start
WORKING-STORAGE SECTION.
01 INPUT-AREA.
get input number
05 INPUT-NUM
PIC 99.
while not end of user input
01 WORK-AREAS.
calculate the answer (multiply input by 2)
05 DOUBLE-NUM PIC 999.
display/print the answer
01 USER-RESPONSE.
get input number
05 ANS-WS
PIC X.
end loop
PROCEDURE DIVISION.
stop
PROCESS.
DISPLAY "ENTER INPUT NUMBER".
ACCEPT INPUT-NUM.
PERFORM UNTIL INPUT-NUM = 99
COMPUTE DOUBLE-NUM = INPUT-NUM * 2
DISPLAY INPUT-NUM "
" DOUBLE-NUM
ACCEPT ANS-WS
DISPLAY "ENTER INPUT NUMBER"
ACCEPT INPUT-NUM
END-PERFORM.
STOP RUN.
IDENTIFICATION DIVISION.
PROGRAM-ID. DOUBLE.
ENVIRONMENT DIVISION.
SELECT NUMBER-FILE
ASSIGN TO "E:\CIS17\COBOL\DOUBLE.TXT".
DATA DIVISION.
FILE SECTION.
FD NUMBER-FILE
DATA RECORD IS NUMBER-RECORD.
01 NUMBER-RECORD.
05 INPUT-NUM
PIC 99.
WORKING-STORAGE SECTION.
01 WORK-AREAS.
Pseudocode:
05 DOUBLE-NUM PIC 999.
01 EOF-CHECK-AREA.
start
05 EOF-IND
PIC X
VALUE "N".
get input number
01 USER-RESPONSE.
05 ANS-WS
PIC X.
while not end of user input
PROCEDURE DIVISION.
calculate the answer (multiply
PROCESS.
display/print the answer
OPEN INPUT NUMBER-FILE.
get input number
READ NUMBER-FILE
end loop
AT END
MOVE "Y" TO EOF-IND.
stop
PERFORM UNTIL EOF-IND = "Y"
COMPUTE DOUBLE-NUM = INPUT-NUM * 2
DISPLAY INPUT-NUM "
" DOUBLE-NUM
ACCEPT ANS-WS
READ NUMBER-FILE
AT END
MOVE "Y" TO EOF-IND
END-READ
END-PERFORM.
STOP RUN.
COBOL
input by 2)
COBOL
This is the file that is being read. You can see on the
output that the first line is for 02 and the second line is
for 12 and the third line is for 25 etc.
VB
Using Visual Basic, I am not
exactly following the logic
flowchart that was laid out.
Visual Basic is event driven. This
means that when the user clicks on
something, it causes certain code
to happen. In this case, I am
having the user enter a number and
then click on Start Program. The
calculation is done and appears in
the box on the left.
There is no loop in this program
because each time the user enters a
number, they click on the Start
Program button and that one
calculation gets done.
VB
Option Explicit
Dim wkDoubleNum As Integer
VB
The variable wkDoubleNum is declared as an
Integer.
Private Sub cmdDoubleFile_Click()
wkDoubleNum = txtInputNum.Text * 2
picDoubleNum.Print "#: "; txtInputNum.Text; "
End Sub
Private Sub cmdEnd_Click()
End
End Sub
When the End button is
clicked this subroutine is
executed and the program
Ends.
Double: "; wkDoubleNum
When the Start Program button is clicked it
executed the cmdDoubleFile subroutine which
does the math and calculates the double amount
which it stores in the variable wkDoubleNum
and then prints the results in the picDoubleNum
box.
VB
Option Explicit
Dim wkDoubleNum As Integer
Private Sub cmdDoubleFile_Click()
wkDoubleNum = txtInputNum.Text * 2
picDoubleNum.Print "#: "; txtInputNum.Text; "
End Sub
Double: "; wkDoubleNum
Private Sub cmdEnd_Click()
End
End Sub
The get input number is handled by
providing a box on the form for the
user to enter the number.
The loop is handled by the user
who enters a number and then
presses the key to Start
Program which does the
routine called cmdDoubleFile.
The user controls the loop with
the click event so no loop is
coded into the program.
Pseudocode:
start
get input number
while not end of user input
calculate the answer (multiply input by 2)
display/print the answer
get input number
end loop
stop
VB
VB
VB
Option Explicit
Dim wkDoubleNum As Integer
Dim wkInputNum As Integer
Private Sub cmdDoubleFile_Click()
Do While Not EOF(1)
Input #1, wkInputNum
wkDoubleNum = wkInputNum * 2
picDoubleNum.Print "#: "; wkInputNum; "Double: "; wkDoubleNum
Loop
MsgBox "Processing Complete"
Close #1
End
End Sub
Private Sub Form_Load()
Open App.Path & "\double.txt" For Input As #1
End Sub
Pseudocode:
start
get input number
while not end of user input
calculate the answer (multiply input by 2)
display/print the answer
get input number
end loop
stop
Pseudocode:
start
while not end of user input
get input number
calculate the answer (multiply input by 2)
display/print the answer
end loop
stop
Download