SS2 WEEK 8 - Sample Question – QBASIC
A school wants to keep student attendance records using a computer program.
Write a QBASIC program to perform the following tasks:
1. Data Entry
Allow the user to enter the following student details:
o Student Name
o Class
o Days Present
2. File Creation
Store the information permanently in a file called: [Link]
3. Append Mode
Allow the user to add another student record to the same file without overwriting the
existing records.
4. File Retrieval
Retrieve all stored records from the file and display them in a table format.
Example Output:
Student Name Class Days Present
John SS1A 20
Mary SS1B 18
Solution with Step-by-Step Explanation
Step 1: Data Entry
Code:
CLS
INPUT "Enter Student Name: ", StudentName$
INPUT "Enter Class: ", StudentClass$
INPUT "Enter Days Present: ", DaysPresent
Explanation:
• CLS clears the screen.
• INPUT allows the user to type in data.
• Three variables are used: StudentName$, StudentClass$, and DaysPresent.
• This part ensures the program collects all necessary information.
Step 2: File Creation
Code:
OPEN "[Link]" FOR OUTPUT AS #1
PRINT #1, StudentName$; ","; StudentClass$; ","; DaysPresent
CLOSE #1
Explanation:
• OPEN ... FOR OUTPUT creates a new file called [Link].
• PRINT #1 writes the student data into the file.
• CLOSE #1 saves and closes the file.
• This ensures the data is permanently stored.
Step 3: Append Mode
Code:
OPEN "[Link]" FOR APPEND AS #1
INPUT "Enter another Student Name: ", StudentName$
INPUT "Enter Class: ", StudentClass$
INPUT "Enter Days Present: ", DaysPresent
PRINT #1, StudentName$; ","; StudentClass$; ","; DaysPresent
CLOSE #1
Explanation:
• FOR APPEND allows adding data without deleting existing records.
• The program asks for new student data and writes it at the end of the file.
Step 4: File Retrieval
Code:
CLS
PRINT "Student Name", TAB(20); "Class", TAB(35); "Days Present"
OPEN "[Link]" FOR INPUT AS #1
DO WHILE NOT EOF(1)
LINE INPUT #1, Record$
Name$ = LEFT$(Record$, INSTR(Record$, ",")-1)
Rest$ = MID$(Record$, INSTR(Record$, ",")+1)
Class$ = LEFT$(Rest$, INSTR(Rest$, ",")-1)
Days$ = MID$(Rest$, INSTR(Rest$, ",")+1)
PRINT Name$; TAB(20); Class$; TAB(35); Days$
LOOP
CLOSE #1
Explanation:
1. CLS clears the screen for display.
2. Column headings are printed with TAB for spacing.
3. OPEN ... FOR INPUT opens the file to read data.
4. DO WHILE NOT EOF(1) loops through all lines until the end of the file.
5. LINE INPUT #1 reads a full line from the file.
6. LEFT$, MID$, INSTR are used to extract Student Name, Class, and Days Present from
the comma-separated record.
7. PRINT ... TAB(...) displays the data in neat columns.
8. CLOSE #1 closes the file.
Step 5: Program Flow Overview
1. Ask user for student attendance data.
2. Save the data permanently in [Link].
3. Optionally, allow additional records using append mode.
4. Display all stored records clearly on the screen.