2015-10-18 63 views
0

我正在学习在我的高中课程中学习基础课程。在学校开始编程之前,我学习了一些C++。我正在编写一个TELEPHONE DIRECTORY程序,即WRITES到或READS FROM"Records.dat"文件。无法找出我的代码错误(Qbasic 64位)

当我运行该程序,并输入"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 
+1

下一次使用四个空格请格式化您的代码 – prasun

+0

Choice $变量未设置,因此WHILE循环未输入。 –

回答

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处理程序来解决此问题。像下面这样的东西应该工作:我最后一个END IF后添加一个END

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 

通知。当你的程序现在,当有人输入无效选项时,你不会做任何事情。我猜这是你稍后将要处理的事情。

相关问题