19.12.2012 Views

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

EXAMPLE 2<br />

The following program uses the file STAFF.TXT in Figure 2-24 to compute weekly pay. Notice that the<br />

variables in the Input # statement are the same types (String, Single, Single) as the constants in each line<br />

of the file.<br />

Private Sub cmdCompute_Click()<br />

Dim nom As String, wage As Single, hrs As Single<br />

picPay.Cls<br />

Open “STAFF.TXT” For Input As #1<br />

Input #1, nom, wage, hrs<br />

picPay.Print nom; hrs * wage<br />

Input #1, nom, wage, hrs<br />

picPay.Print nom; hrs * wage<br />

Close #1<br />

End Sub<br />

[Run, <strong>and</strong> then click the comm<strong>and</strong> button. The following will be displayed in the picture box.]<br />

Mike Jones 257.25<br />

John Smith 222.75<br />

In certain situations, we must read the data in a file more than once. This is accomplished<br />

by closing the file <strong>and</strong> reopening it. After a file is closed <strong>and</strong> then reopened, subsequent<br />

Input # statements begin reading from the first entry of the file.<br />

EXAMPLE 3<br />

The following program takes the average annual amounts of money spent by single-person households for<br />

several categories <strong>and</strong> converts these amounts to percentages. The data are read once to compute the total<br />

amount of money spent <strong>and</strong> then read again to calculate the percentage for each category. Note: These<br />

figures were compiled for the year 1995 by the Bureau of Labor Statistics.<br />

COSTS.TXT consists of the following four lines:<br />

“Transportation”, 3887<br />

“Housing”, 7643<br />

“Food”, 3017<br />

“Other”, 7804<br />

Private Sub cmdCompute_Click()<br />

Dim total As Single, category As String, amount As Single<br />

Open “COSTS.TXT” For Input As #1<br />

picPercent.Cls<br />

total = 0<br />

Input #1, category, amount<br />

total = total + amount<br />

Input #1, category, amount<br />

total = total + amount<br />

Input #1, category, amount<br />

total = total + amount<br />

Input #1, category, amount<br />

total = total + amount<br />

Close #1<br />

Open “COSTS.TXT” For Input As #1<br />

Input #1, category, amount<br />

picPercent.Print category; amount / total<br />

Input #1, category, amount<br />

picPercent.Print category; amount / total<br />

Input #1, category, amount<br />

Input <strong>and</strong> Output 51

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!