Input and Output - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

Input and Output

Description:

The White House is located at 1600 Pennsylvania Ave. Using a Message Box for Output ... now have a choice of using the mouse to click on a button or using a ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 24
Provided by: mariamoni
Category:

less

Transcript and Presenter's Notes

Title: Input and Output


1
Input and Output
  • Reading Data From Files
  • Data files can be created with Windows NotePad,
    MSWord, or any another text editor. It is
    important to save the files as an text only,or
    text only with line breaks.
  • A file can have either one item per line or
    many items (separated by commas) can be listed on
    the same line.
  • Example the following file contains payroll
    information. Each line contains the name of a
    person, that persons hourly wage, and the number
    of ours that person worked during the week.

2
  • ______________________________________
  • Mike Jones, 7.35, 35
  • John Smith, 6.75, 33
  • ________________________________
  • Data stored in a file can be read in order (
    that is, sequentially) and assigned to variables
    with the following steps
  • Choose a number from 1 to 255 to be the reference
    number for the file
  • Execute the statement
  • Open filenameFor Input As n
  • Where n is the reference number
  • The above procedure is referred to as opening a
    file for input. It establishes a communication
    link between the computer and the disk drive for
    reading data from the disk.

3
  • Read items of data in order, one at item, from
    the file with input statements. The statement
  • Input n, var
  • Causes the program to look in the file for the
    next available item of data and assign it to the
    variable var.
  • In the file individual items are separated by
    commas or line breaks. The variable in the Input
    statement must be the same type as the data to
    be assigned to it from the file.
  • 4. After the desired items have been read from
    the file, close the file with the statement
  • Close n

4
  • Note A statement of the form
  • Input n, var1, var2, , varj
  • Has the same effect as the sequence of staments
  • Input n, var1
  • Input n, var2
  • .
  • Input n, varj
  • Example The following program takes the average
    annual amounts of money spent by single-person
    households for several categories and converts
    these amounts to percentages.
  • The form design for this example consists of a
    command button and a picture box.

5
  • _______________________________
  • Transportation, 3887
  • Housing, 7643
  • Food, 3017
  • Other, 7804
  • _______________________________

6
  • Private Sub cmdCompute_Click()
  • Dim total As Single, category As string, amount
    As Single
  • Open COSTS.TXT For Input As 1
  • PicPercent.Cls
  • total0
  • Input 1, category, amount
  • total totalamount
  • Input 1, category, amount
  • total totalamount
  • Input 1, category, amount
  • total totalamount
  • Input 1, category, amount
  • total totalamount
  • Close 1
  • Open COSTS.TXT For Input As 1
  • Input 1, category, amount
  • PicPercent.Print category amount/total
  • Input 1, category amount
  • PicPercent.Print category amount/total

7
  • Run and then click the command button. The
    following is displayed in the picture box
  • Transportation 0.1739072
  • Housing 0.3419534
  • Food 0.1349828
  • Other 0.3491566
  • Input from a Input Box
  • Normally, a text box is used to obtain input
    described by a label. However, in some cases it
    is not practical to have a text box and label on
    the screen permanently.
  • When a statement of the form
  • stringVarInputBox(prompt, title)
  • Is executed, an input box like this

8
Pops up on the screen. After user types a
response into the text box at the bottom of
screen and press Enter (or clicks OK), the
response is assigned to the string variable
stringVar.
9
  • The title argument is optional and gives the
    caption to appear in title bar. The prompt
    argument is a string that ells the user what
    information to type into the text box.
  • ExampleIn this example the file name is provided
    by the user in an input box.
  • Run, and then click the command button. An input
    box appears on the screen. Type Data.Txt into the
    input box and click OK.
  • ________________________
  • 1600
  • Pennsylvania Ave.
  • ________________________
  • Data.Txt

10
  • Private Sub cmdDisplay_Click()
  • Dim fileName as String, prompt As String, title
    As String
  • Dim houseNumber As Single, street As String
  • picAddress.Cls
  • promptEnter the name of the file containing
    the information.
  • Title Name of the File
  • fileNameInputBox(prompt, title)
  • Open file Name For Input As 1
  • Input 1, houseNumber
  • Input 1, street
  • picAddress.Print The white House is located
    at houseNumber street
  • Close 1
  • End Sub
  • The White House is located at 1600 Pennsylvania
    Ave.

11
  • Using a Message Box for Output
  • Some times you want to grab the users attention
    with a brief message such ascorrect. You want
    this message only to appear on the screen until
    the user has read it.
  • When a statement of the form
  • MsgBox prompt, , title
  • Is executed, where prompt and title are strings,
    a message box with prompt displayed and the title
    bar caption title appears, and stay on the screen
    until the users presses Enter or clicks OK.
    Example
  • MsgBox "Enter the name of a file", , "hi!
  • produces

12
(No Transcript)
13
Example
  • The following program is going to use input
    boxes to allow the user to enter their name and
    age. It will do a simple calculation and print
    the results to a label. 
  • The form looks like this

14
  •   Build the form according to the following
    specification
  • Form Name frmAge
  • Caption Age
  • Command Button Name cmdExit
  • Caption Exit
  • Command Button Name cmdDetails
  • Caption EnterDetails
  • Command Button Name cmdResults
  • Caption Print Results
  • Label Name lblResults
  • Border Style Fixed Single
  • Caption none

15
  • Notice that the Caption property for each of the
    command buttons has an ampersand - a ''
    character - in it and this has the effect of
    underlining the letter that follows it. When you
    run the program, users now have a choice of using
    the mouse to click on a button or using a key
    combination. Let's see how it works...
  • 1. Enter the code for the Exit button in the
    usual way
  • 2. Run the program
  • 3. Hold the ALT key and press the 'x' key
  • So holding the ALT key and pressing the
    underlined letter has the same effect as clicking
    the button with a mouse. Clearly you must make
    sure that each button has a different letter
    that's underlined - you can't have two buttons
    with the same key combination.

16
  • we have two variables. The user's name is a
    string, or text, variable and we will call it
    UserName. The person's age is an integer and we
    will call it Age.
  • If you've ever used a typewriter - well, we did
    say history - you'll know that when you want to
    start a new line, you have to do a line feed and
    a carriage return. The first of these moves the
    paper up a line and the second moves you to the
    start of the line.
  • The upshot is that we really need to declare
    another variable called LFCR (Line Feed, Carriage
    Return) which we can use to do the job for us.
    You'll see how it works a little later but for
    now we need to declare all three variables in the
    General Declarations section of the project.
  • Declare the variables like this

17
  • Here's the code for the cmdDetails Click event
  • Private Sub cmdDetails_Click()
  • UserName InputBox("Please enter your name")
  • Age InputBox("please enter your age")
  • End Sub
  • Type in the code
  • Run the program

18
  • Let's move now to the code for the Click event of
    the cmdResults button.
  • Private Sub cmdResults_Click()
  • Dim Days As Integer
  • Dim TextOut As String
  • LFCR Chr(10) Chr(13) 
  • Days 365 Age
  • 'build output string
  • TextOut "Hello " UserName
  • TextOut TextOut LFCR
  • TextOut TextOut "You are at least "
  • TextOut TextOut Str(Days)
  • TextOut TextOut " days old!"
  • lblResults.Caption TextOut
  • End Sub

19
  • Enter the code and save your work
  • Run the program and note what happens
  • OK, let's go through this code, starting with
    these two lines
  • Dim Days As Integer
  • Dim TextOut As String
  •  
  • Nothing too mysterious here. We've declared Days
    as an integer and this is what we use for the
    person's age in days. We've also declared TextOut
    as a string and this is what we're going to use
    as the message to be printed out.

20
  • The line 
  •  
  • LFCR Chr(10) Chr(13)
  • needs some explanation. Chr(10) is the linefeed
    character and Chr(13) is the carriage return
    character and so LFCR is a combination of the
    two, which we need when we print out the text in
    the label. All it does is move any following text
    onto a new line, just as if you'd pressed the
    ENTER key.
  • Days 365 Age
  • This calculates the number of days assuming that
    there are 365 days in a year

21
  •  The next line
  • 'build output string
  • doesn't in fact do anything and is ignored by the
    computer. It does however act as a comment to us
    and it is considered good practice to use
    comments in order to help other people understand
    your programs and make them more readable.
  • And now to the following code
  • TextOut "Hello " UserName
  • TextOut TextOut LFCR
  • TextOut TextOut "You are at least "
  • TextOut TextOut Str(Days)
  • TextOut TextOut " days old!"

22
  • The comment said it all really. We are building
    up a string, or piece of text, step by step. We
    start with the word "Hello " and we add the
    variable UserName, which we know is another piece
    of text
  • . The line
  • TextOut TextOut LFCR
  •  
  •  takes the string TextOut and adds to it our
    linefeed/carriage return combination. We proceed
    to add to the TextOut string as we go. There is a
    slight problem because, while UserName is a
    string, Days is an integer, and it doesn't make
    sense to add a number to a string - they are
    completely different data types. The way round
    it, though, is to use a special function called
    Str which Visual Basic has thoughtfully provided.
    It's job is to take a numerical value and convert
    it into a string, which we can then add to
    TextOut.

23
  • Finally, the line
  • lblResults.Caption TextOut
  • assigns our completed string to the Caption
    property of the label, and that's what you see
    printed out.
Write a Comment
User Comments (0)
About PowerShow.com