Qbasicnews.com
December 15, 2019, 05:51:09 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Poll
Question: is this question stupid? (hint: click yes!)  (Voting closed: October 23, 2002, 10:25:34 AM)
yes - 4 (100%)
Total Voters: 11

Pages: [1]
  Print  
Author Topic: Error meesages  (Read 8082 times)
somekid
New Member

Posts: 17


WWW
« 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?! Shocked
Logged
red_Marvin
Na_th_an
*****
Posts: 1509



WWW
« Reply #1 on: October 23, 2002, 10:59:21 AM »

It's possible to do that with ON ERROR
-Read about ON ERROR...
Logged

/post]
Sumo Jo
Been there, done that
*****
Posts: 1244



WWW
« Reply #2 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:
Logged

url=http://fileanchor.com]FileAnchor[/url] - ImageAnchor - FBTK - QbasicNews - VPlanet - Various
somekid
New Member

Posts: 17


WWW
« Reply #3 on: October 24, 2002, 09:42:03 AM »

how would i go about using the ON ERROR GOTO command? :Huh:

i have absolutely no experience with error trapping what-so-ever...
Logged
BlueKeyboard
Wandering Guru
***
Posts: 337



WWW
« Reply #4 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 Smiley.
Logged

Neo
Na_th_an
*****
Posts: 2150



« Reply #5 on: October 28, 2002, 08:00:51 AM »

Very good, however, don't overuse the ON ERROR commands, it makes your code quite slow...
Logged
somekid
New Member

Posts: 17


WWW
« Reply #6 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 :Huh: ?

i am assuming you would use an if command?

thanks for the help! :lol:
Logged
BlueKeyboard
Wandering Guru
***
Posts: 337



WWW
« Reply #7 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.
Logged

somekid
New Member

Posts: 17


WWW
« Reply #8 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! :Huh:
Logged
BlueKeyboard
Wandering Guru
***
Posts: 337



WWW
« Reply #9 on: October 30, 2002, 02:27:08 PM »

Exactly what line causes the problem?.
Logged

Neo
Na_th_an
*****
Posts: 2150



« Reply #10 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! :Huh:


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
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!