Qbasicnews.com

QBasic => QB Discussion & Programming Help => Topic started by: somekid on October 23, 2002, 10:25:34 AM



Title: Error meesages
Post by: somekid on October 23, 2002, 10:25:34 AM
in one of my programs, the user is asked to enter a file name and if they enter a name that does not exist the program exists and goes to the error screen in qbasic...

Is it possible to over-ride that function and create error messages within the program that displays the closest matching filename and asks the user if they would like to open that instead?! :o


Title: Error meesages
Post by: red_Marvin on October 23, 2002, 10:59:21 AM
It's possible to do that with ON ERROR
-Read about ON ERROR...


Title: You could always use some kind of trap
Post by: Sumo Jo on October 23, 2002, 08:17:13 PM
You could use if/then to make sure the file inputted is real, if it wasn't then you could print something out to the screen saying that the file they entered wasn't correct, try again.   Error trapping, it makes the program tell the user something is wrong before the computer does.

 :bounce:


Title: Error meesages
Post by: somekid on October 24, 2002, 09:42:03 AM
how would i go about using the ON ERROR GOTO command? :???:

i have absolutely no experience with error trapping what-so-ever...


Title: Error meesages
Post by: BlueKeyboard on October 24, 2002, 10:01:24 AM
Like this:
Code:

ON ERROR GOTO ErrorHandler

INPUT "File:"; File$

OPEN File$ FOR INPUT AS #1
CLOSE #1

ErrorHandler:
SELECT CASE ERR
CASE 53
PRINT "File not found."
CASE 64
PRINT "The file name you entered was incorrect."
CASE 76
PRINT "Path not found."
END SELECT

Try typing in weird filenames and paths, and see what happens :).


Title: Error meesages
Post by: Neo on October 28, 2002, 08:00:51 AM
Very good, however, don't overuse the ON ERROR commands, it makes your code quite slow...


Title: if statements for error trapping
Post by: somekid on October 30, 2002, 11:16:38 AM
what would i have to do if i wanted it to create a new file if the filename was not found using the same name as the file that was not found :???: ?

i am assuming you would use an if command?

thanks for the help! :lol:


Title: Error meesages
Post by: BlueKeyboard on October 30, 2002, 11:23:21 AM
You mean, if the file which the user entered is not found, the file should be created?, if so:
Code:

ON ERROR GOTO ErrorHandler

INPUT "File:"; File$

OPEN File$ FOR INPUT AS #1
CLOSE #1

ErrorHandler:
SELECT CASE ERR
CASE 53
PRINT "File not found."
PRINT "Creating file:"; File$
OPEN File$ FOR OUTPUT AS #1
CLOSE #1
RESUME
CASE 64
PRINT "The file name you entered was incorrect."
CASE 76
PRINT "Path not found."
END SELECT
' untested

Ok, this is what it does:
If the file was not found, it prints out "File not found.", "Creating file: Somefile"
Then it creates the file, and tries to reopen the file again, using RESUME.
RESUME resumes the code line where the error occured.


Title: Error meesages
Post by: somekid on October 30, 2002, 11:43:18 AM
ok, i used the code you suggested and now it creates the new file! :rotfl:

however, a new problem has popped up.  it creates the file, then it opens the file and for some reason it goes to the errorhandler and freezes there.

here is the sub where the problem occurs:
Code:

SUB SELECTSTUDENT
'THIS SUB WILL PROMPT THE USER FOR THE STUDENTS NAME, WHICH DOUBLES AS A FILE NAME IN WHICH THE DATA IS STORED

ON ERROR GOTO ERRORHANDLER

PRINT "PLEASE ENTER THE STUDENTS NAME"
INPUT N$
OPEN (N$) + ".SDN" FOR INPUT AS #1
STUDENTINFO

END SUB


i also modified the error handler to comply with my requirements:
Code:
ERRORHANDLER:
SELECT CASE ERR
CASE 53
PRINT "File not found."
PRINT "Creating file:"; N$
OPEN N$ + ".SDN" FOR OUTPUT AS #1
CLOSE #1
SELECTSTUDENT
CASE 64
PRINT "The file name you entered was incorrect."
CASE 76
PRINT "Path not found."
END SELECT
RESUME

i am really lost and so any help is apreciated! :???:


Title: Error meesages
Post by: BlueKeyboard on October 30, 2002, 02:27:08 PM
Exactly what line causes the problem?.


Title: Error meesages
Post by: Neo on October 31, 2002, 07:52:52 AM
Quote from: "somekid"
ok, i used the code you suggested and now it creates the new file! :rotfl:

however, a new problem has popped up.  it creates the file, then it opens the file and for some reason it goes to the errorhandler and freezes there.

here is the sub where the problem occurs:
Code:

SUB SELECTSTUDENT
'THIS SUB WILL PROMPT THE USER FOR THE STUDENTS NAME, WHICH DOUBLES AS A FILE NAME IN WHICH THE DATA IS STORED

ON ERROR GOTO ERRORHANDLER

PRINT "PLEASE ENTER THE STUDENTS NAME"
INPUT N$
OPEN (N$) + ".SDN" FOR INPUT AS #1
STUDENTINFO

END SUB


i also modified the error handler to comply with my requirements:
Code:
ERRORHANDLER:
SELECT CASE ERR
CASE 53
PRINT "File not found."
PRINT "Creating file:"; N$
OPEN N$ + ".SDN" FOR OUTPUT AS #1
CLOSE #1
SELECTSTUDENT
CASE 64
PRINT "The file name you entered was incorrect."
CASE 76
PRINT "Path not found."
END SELECT
RESUME

i am really lost and so any help is apreciated! :???:


I think you're having some problems with the scope the N$ variable. Look: the errortrap must be in the main module, while the error occured in a sub, with a variable N$, in the main module there is no N$. I think you could solve this problem with:

Code:

SUB SELECTSTUDENT
'THIS SUB WILL PROMPT THE USER FOR THE STUDENTS NAME, WHICH DOUBLES AS A FILE NAME IN WHICH THE DATA IS STORED

ON ERROR GOTO ERRORHANDLER

PRINT "PLEASE ENTER THE STUDENTS NAME"
INPUT N$
OPEN (N$) + ".SDN" FOR INPUT AS #1
STUDENTINFO

END SUB


Main Module:
Code:
'$DYNAMIC
DEFINT A-Z
DIM SHARED N$

CALL SELECTSTUDENT

ERRORHANDLER:
SELECT CASE ERR
CASE 53
PRINT "File not found."
PRINT "Creating file:"; N$
OPEN N$ + ".SDN" FOR OUTPUT AS #1
CLOSE #1
SELECTSTUDENT
CASE 64
PRINT "The file name you entered was incorrect."
CASE 76
PRINT "Path not found."
END SELECT
RESUME