0

I am learning to program in Basic as course in my high school. I have learned some C++ before programming started in school. I was writing a TELEPHONE DIRECTORY program that WRITES to or READS FROM the "Records.dat" File.

When I run the program, and enter "Q" to Exit, it works fine. But if I Enter "E" to input new records in APPEND MODE or "O" to enter records in OUTPUT mode or "V" to view records, the program does not do anything. It creates the file and does not hang, but it shows no output. Here is the code:

10 CLS
20 Name$ = "0": Number$ = "0": Adrs$ = "0": Choice$ = "0": Mode$ = "0": Records = 0: Space = 0
30 PRINT "Telephone Directory Program."; "Press 'E' to Enter new records in Existing File"; "Press 'V' to View existing records"; "Press 'Q' to Exit"; "IF THERE ARE NO RECORDS PRESS O";
40 INPUT Mode$
50 IF Mode$ = "Q" THEN
    END
ELSEIF Mode$ = "E" THEN
    CLS
    OPEN "Records.dat" FOR APPEND AS #1
    ON ERROR GOTO 30
    PRINT "Enter Records when prompted.";
    WHILE Choice$ = "Y" OR Choice$ = "y"
        INPUT "Enter Name: ", Name$
        INPUT "Enter Phone Number: ", Number$
        INPUT "Enter Address: ", Adrs$
        WRITE #1, Name$, Number$, Adrs$
        INPUT "IF you want to enter new records, enter Y or y. Otherwise, press any other letter. ", Choice$
    WEND
    CLOSE #1
    GOTO 10
ELSEIF Mode$ = "O" THEN
    CLS
    OPEN "Records.dat" FOR OUTPUT AS #2
    PRINT "Enter Records when prompted.";
    WHILE Choice$ = "Y" OR Choice$ = "y"
        INPUT "Enter Name: ", Name$
        INPUT "Enter Phone Number: ", Number$
        INPUT "Enter Address: ", Adrs$
        WRITE #1, Name$, Number$, Adrs$
        INPUT "IF you want to enter new records, enter Y or y. Otherwise, press any other letter. ", Choice$
    WEND
    CLOSE #2
    GOTO 10
ELSEIF Mode$ = "V" THEN
    CLS
    OPEN "Records.dat" FOR INPUT AS #3
    PRINT SPC(24), "Directory Listing";
    WHILE NOT EOF(3)
        Records = Records + 1
    WEND
    IF Records = 0 THEN
        PRINT "NO RECORDS FOUND. ENTER O AT THE NEXT SCREEN";
        GOTO 10
    ELSE
        PRINT "Names", SPC(5), "Phone Numbers ", SPC(6), "Addresses";
        WHILE NOT EOF(3)
            INPUT #3, Name$, Number$, Adrs$
            PRINT Name$
            Space = (10 - (LEN(Name$)))
            PRINT SPC(Space)
            PRINT Number$
            Space = (20 - (LEN(Number$)))
            PRINT SPC(Space)
            PRINT Adrs$;
        WEND
        PRINT ;
        PRINT Records, " Records found";
        CLOSE #3
        GOTO 10
    END IF
END IF

1 Answers1

1
WHILE Choice$ = "Y" OR Choice$ = "y"

You should initialize Choice$ to "Y" instead of "0" for entering records. Otherwise, the WHILE-WEND is skipped because there is no place to enter a value for Choice$ before that loop. Then the file closes, and the program restarts with GOTO 10.

When viewing the records, you open the file and want to count the records. However, WHILE NOT EOF(3) will run forever; you don't perform any input operations on the file, so it will never reach the end of the file. If there are no records, don't forget to CLOSE #3 before GOTO 10.

You might also get a "File not found" error if you view (V) before the database is created. You can use a special ON ERROR handler to fix this. Something like the following should work:

    ON ERROR GOTO 900
    OPEN "Records.dat" FOR INPUT AS #3
    ON ERROR GOTO 0
    PRINT SPC(24), "Directory Listing";
    .
    .
    .
    END IF
END IF
END

900 PRINT "Records.dat not found; create it by entering records"
RESUME 10

Notice that I added an END after the last END IF. As your program is now, you don't do anything when someone types an invalid option. I'm guessing that's something you'll work on later.