我正在学习基础课作为高中课程。我在学校开始编程之前学过一些C ++。我正在撰写一个TELEPHONE DIRECTORY
计划WRITES
或READS 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
答案 0 :(得分: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
。由于您的程序现在是,当有人键入无效选项时,您不会执行任何操作。我猜这是你以后会做的事情。