【问题标题】:Cannot Figure out the error with my code(Qbasic 64bit)无法用我的代码找出错误(Qbasic 64bit)
【发布时间】:2015-10-18 04:58:23
【问题描述】:

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

当我运行程序并输入"Q" 以退出时,它工作正常。但是,如果我输入"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

【问题讨论】:

  • 请下次使用四个空格格式化您的代码
  • Choice$ 变量未设置,因此未进入 WHILE 循环。

标签: windows basic qbasic data-files gw-basic


【解决方案1】:
WHILE Choice$ = "Y" OR Choice$ = "y"

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

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

如果您在创建数据库之前查看 (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 IF 之后添加了一个 END。就像您现在的程序一样,当有人键入无效选项时,您不会做任何事情。我猜这是你以后会做的事情。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-06
    • 2016-07-10
    • 1970-01-01
    • 2018-06-30
    • 1970-01-01
    • 1970-01-01
    • 2021-07-18
    • 1970-01-01
    相关资源
    最近更新 更多