Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSIT 208, Section 32301 Instructor: P eter C hen Introduction to Programming with QBasic to Visual Basic Lecture 9.

Similar presentations


Presentation on theme: "CSIT 208, Section 32301 Instructor: P eter C hen Introduction to Programming with QBasic to Visual Basic Lecture 9."— Presentation transcript:

1 CSIT 208, Section 32301 Instructor: P eter C hen Introduction to Programming with QBasic to Visual Basic Lecture 9

2 CSIT 208, Section 32302 Example 4: Write a program to display a table showing the ages in 1999 of the people in the sequential file YOB.DAT. CLS OPEN “YOB.DAT” FOR INPUT AS #1 PRINT “Name”, “Age in 1999” DO WHILE NOT EOF(1) INPUT #1, name$, year PRINT name$, 1999 – year LOOP CLOSE #1 END

3 CSIT 208, Section 32303 Exercise Continue from example #4, print out the person whose age is greater than 55.

4 CSIT 208, Section 32304 Example 5: Write a program to search the file YOB.DAT for the year of birth of a specific person. CLS INPUT “Person (Capitalize first letter only) “; person$ OPEN “YOB.DAT” FOR INPUT AS #1 LET name$ = “ “ DO WHILE (name$ <> person$) AND (NOT EOF(1)) INPUT #1, name$, year LOOP IF name$ = person$ THEN PRINT person$; “ was born in “ year ELSE PRINT person$; “ is not in the file YOB.DAT” END IF CLOSE #1 END

5 CSIT 208, Section 32305 Exercise Write a program to search the file YOB.DAT for the name of the person of a specific year.

6 CSIT 208, Section 32306 Fixed Length Strings In this example, San Francisco is truncated to a string of length 9 and Detroit is padded on the right with two blink spaces. CLS DIM city AS STRING * 9 LET city = “San Francisco” PRINT city LET city = “Detroit” PRINT city; “MI” PRINT LEN(city) END

7 CSIT 208, Section 32307 Records A Record is a user-defined data type that groups related variables of different types. The layout of the record is declared by the block of the statements TYPE collegeData nom AS STRING * 30 state AS STRING * 2 yearFound AS SINGLE END TYPE A record variable capable of holding the data for a specific college Is declared by a statement such as DIM college AS collegeData

8 CSIT 208, Section 32308 CLS TYPE collegeData nom AS STRING * 30 state AS STRING * 2 yearFound AS SINGLE END TYPE DIM college AS collegeData INPUT “Name”; college.nom INPUT “State”: college.state INPUT “Year Founded”; college.yearFounded LET century = 1 + INT(college.yearFounded / 100) PRINT RTRIM(college.nom); “ was found in the century; PRINT “th century in “; college.state END * Page 336

9 CSIT 208, Section 32309 Exercise Define a record type PersonData which has three fields, first name, last name and date of birth. First name and last name are string of 20, DOB is assigned as single. You program will ask for input first name, last name, and DOB. Output message will be : Hi (first name) (last name)! You are (age) year old.


Download ppt "CSIT 208, Section 32301 Instructor: P eter C hen Introduction to Programming with QBasic to Visual Basic Lecture 9."

Similar presentations


Ads by Google