0

我正在学习基础编程作为我高中的课程。在学校开始编程之前,我已经学习了一些 C++。我正在编写一个用于或文件的TELEPHONE DIRECTORY程序。WRITESREADS FROM"Records.dat"

当我运行程序并进入"Q"退出时,它工作正常。但是,如果我 Enter"E"以输入新记录APPEND MODE"O"OUTPUT模式输入记录或"V"查看记录,则程序不会执行任何操作。它会创建文件并且不会挂起,但不会显示任何输出。这是代码:

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
4

1 回答 1

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

您应该初始化Choice$"Y"而不是"0"输入记录。否则,将跳过 ,因为在该循环之前WHILE-WEND没有地方可以输入值。Choice$然后文件关闭,程序以GOTO 10.

查看记录时,您打开文件并想要计算记录。但是,WHILE NOT EOF(3)将永远运行;您不对文件执行任何输入操作,因此它永远不会到达文件末尾。如果没有记录,不要忘记CLOSE #3之前GOTO 10

如果您在创建数据库之前查看 (V),您还可能会收到“找不到文件”错误。您可以使用特殊的 ON ERROR 处理程序来解决此问题。像下面这样的东西应该可以工作:

    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

请注意,我END在最后一个END IF. 就像您现在的程序一样,当有人键入无效选项时,您不会做任何事情。我猜这是你以后会做的事情。

于 2015-10-18T14:40:07.477 回答