Qbasicnews.com

QBasic => QB Discussion & Programming Help => Topic started by: Liquid Snake on March 01, 2005, 03:35:13 PM



Title: "Open" Problem
Post by: Liquid Snake on March 01, 2005, 03:35:13 PM
Here's a portion of code for a game I'm making.  I keep getting an error "input past end of file"

Code:

LOAD:
PRINT "not done yet"
CHOICE = 0
OPEN "fballsav.txt" FOR INPUT AS #1
WHILE NOT EOF(1)
INPUT #1, qb, kicker, seasons, TOTALWON, TOTALLOST, WON, LOST, NAME$, TRAIN
WEND
CLOSE
RETURN

SAVE:
PRINT "NOT DONE YET"
CHOICE = 0
OPEN "fballsav.txt" FOR OUTPUT AS #1
PRINT #1, qb, kicker, seasons, TOTALWON, TOTALLOST, WON, LOST, NAME$, TRAIN
CLOSE
RETURN


Title: "Open" Problem
Post by: TheDarkJay on March 01, 2005, 04:02:16 PM
{DOUBLE POST}(SORRY)


Title: "Open" Problem
Post by: TheDarkJay on March 01, 2005, 04:03:24 PM
The info is not there for you to load, if you have a txt file with not as many numbers as there are there, it will not work.


Title: "Open" Problem
Post by: Liquid Snake on March 01, 2005, 05:37:17 PM
That doesn't seem to be the case, because I have 9 variables and 9 values in my save file.  Here's what it looks like:

50          50        0          0           0        0        0     name       0


Title: "Open" Problem
Post by: Mitth'raw'nuruodo on March 02, 2005, 01:22:12 AM
I think It's the loop.

try:
DO
IF EOF(1) = -1 THEN EXIT DO
INPUT #1, blah blah blah
LOOP

That will work as long as the txt file is correct which you said ot was.


Title: "Open" Problem
Post by: Neo on March 03, 2005, 06:54:48 AM
NAME is a restricted variable name.

#1. Try using something different, like playername$
#2. Make sure the playername$ doesn't contain commas, semicolons or spaces.

Just some hints though, because I'm not able to test it atm :)


Title: "Open" Problem
Post by: Anonymous on March 03, 2005, 08:31:39 AM
learn binary =P


Title: "Open" Problem
Post by: barok on March 03, 2005, 10:47:15 AM
i think your problem is your while loop.  It loops, then when it reaches the end of file it loops once more.  Unfortunately, it has nothing else to read, so you got your error.  use a do-loop, or use LOF() to find the length of the file, then run a for...next loop.


Title: "Open" Problem
Post by: Neo on March 03, 2005, 06:14:51 PM
WHILE NOT EOF should work fine.


Title: "Open" Problem
Post by: pr0gger on March 03, 2005, 06:19:52 PM
[syntax="qbasic"]OPEN "fballsav.txt" FOR INPUT AS #1
WHILE NOT EOF(1)
INPUT #1, qb, kicker, seasons, TOTALWON, TOTALLOST, WON, LOST, NAME$, TRAIN
WEND
CLOSE[/syntax]

I think your problem his here as well, but not necessarily with the WHILE loop.  Since the "EOF" statement is at the top, it should evaluate that everytime it restarts the loop-- so if it reaches the end of the file, it should simply stop.  I think your problem somewhere within the input statement:

[syntax="qbasic"]INPUT #1, qb, kicker, seasons, TOTALWON, TOTALLOST, WON, LOST, NAME$, TRAIN[/syntax]

What may be happening is you might have a file that looks like this:

Code:
50 50 0 0 0 0 0 name 0



With an extra blank line at the end of the file.  If there is one, delete it.

If that doesn't work, try adding commas so your file looks like this:

Code:
50,50,0,0,0,0,0,"name",0


You'll still get the same result, but it should be easier to work with.

Good luck!

--j_k


Title: "Open" Problem
Post by: Ralph on March 03, 2005, 09:37:33 PM
As one final suggestion, just after your line:

INPUT #1, qb, kicker, seasons, TOTALWON, TOTALLOST, WON, LOST, NAME$, TRAIN

add this one:

PRINT "qb =";qb;", kicker =";kicker;", seasons =";seasons";", TOTALWON =";TOTALWON;", LOST =";LOST;", NAME$ = ";NAME$;", TRAIN=";TRAIN

Now, you can see where your problem begins, so you can do whatever correction your probgram requires!  Good luck.


Title: "Open" Problem
Post by: Liquid Snake on March 04, 2005, 04:02:46 PM
Thanks, I'll give it a try when I get a chance.


Title: "Open" Problem
Post by: Liquid Snake on March 05, 2005, 06:15:08 PM
Changing the variable "name$" didn't make any difference.
The loop doesn't seem to be the problem either.


Title: "Open" Problem
Post by: Liquid Snake on March 05, 2005, 06:54:33 PM
Quote from: "Ralph"
As one final suggestion, just after your line:

INPUT #1, qb, kicker, seasons, TOTALWON, TOTALLOST, WON, LOST, NAME$, TRAIN

add this one:

PRINT "qb =";qb;", kicker =";kicker;", seasons =";seasons";", TOTALWON =";TOTALWON;", LOST =";LOST;", NAME$ = ";NAME$;", TRAIN=";TRAIN

Now, you can see where your problem begins, so you can do whatever correction your probgram requires!  Good luck.



I can't right after that line because the problem comes up ON THAT LINE, so it doesn't go past it.


Title: "Open" Problem
Post by: Liquid Snake on March 05, 2005, 07:06:15 PM
Quote from: "pr0gger"
[syntax="qbasic"]What may be happening is you might have a file that looks like this:

Code:
50 50 0 0 0 0 0 name 0



With an extra blank line at the end of the file.  If there is one, delete it.

If that doesn't work, try adding commas so your file looks like this:

Code:
50,50,0,0,0,0,0,"name",0


You'll still get the same result, but it should be easier to work with.

Good luck!

--j_k


How do you input commas into the file?


Title: "Open" Problem
Post by: Liquid Snake on March 05, 2005, 07:21:37 PM
Quote from: "Mitth'raw'nuruodo"
I think It's the loop.

try:
DO
IF EOF(1) = -1 THEN EXIT DO
INPUT #1, blah blah blah
LOOP

That will work as long as the txt file is correct which you said ot was.


Unfortuanately, that wasn't the case either


Title: "Open" Problem
Post by: Ralph on March 05, 2005, 07:38:05 PM
Quote from: "Liquid Snake"
Quote from: "Ralph"
As one final suggestion, just after your line:

INPUT #1, qb, kicker, seasons, TOTALWON, TOTALLOST, WON, LOST, NAME$, TRAIN

add this one:

PRINT "qb =";qb;", kicker =";kicker;", seasons =";seasons";", TOTALWON =";TOTALWON;", LOST =";LOST;", NAME$ = ";NAME$;", TRAIN=";TRAIN

Now, you can see where your problem begins, so you can do whatever correction your probgram requires!  Good luck.



I can't right after that line because the problem comes up ON THAT LINE, so it doesn't go past it.


O.K., then, try dissecting your INPUT and PRINT statements:

INPUT #1, qb:PRINT "qb =";qb
INPUT#1, kicker:PRINT "kicker =";kicker
etc.

Now, you should be able to see at what point the INPUT has a problem.


Title: "Open" Problem
Post by: Liquid Snake on March 05, 2005, 08:05:46 PM
ok I'll give it a try, but I'm open to any other suggestions as well.
Would I still need the loop if I try what you suggested?

By the way, is the OPEN statement the easiest way to do this? Or is there a better way?


Title: "Open" Problem
Post by: Ralph on March 05, 2005, 08:39:51 PM
Quote from: "Liquid Snake"
ok I'll give it a try, but I'm open to any other suggestions as well.
Would I still need the loop if I try what you suggested?

By the way, is the OPEN statement the easiest way to do this? Or is there a better way?

Yes, you will maintain all your code just as it is, except for modifying the INPUT #1 statement to only one entry at a time, followed by the addition of a PRINT statement with the expected entry, so you can see at which point the problem begins.  Me, I am only trying to help you find the problem you have.


Title: "Open" Problem
Post by: Liquid Snake on March 05, 2005, 08:41:20 PM
Quote from: "Ralph"
Quote from: "Liquid Snake"
ok I'll give it a try, but I'm open to any other suggestions as well.
Would I still need the loop if I try what you suggested?

By the way, is the OPEN statement the easiest way to do this? Or is there a better way?

Yes, you will maintain all your code just as it is, except for modifying the INPUT #1 statement to only one entry at a time, followed by the addition of a PRINT statement with the expected entry, so you can see at which point the problem begins.  Me, I am only trying to help you find the problem you have.


The problem occurs at the last variable.


Title: "Open" Problem
Post by: Liquid Snake on March 05, 2005, 09:18:02 PM
I know what the problem is now. The last variable is being read as part of the second to last variable. So, I made the string the last variable,   the problem is gone.  However, how can I avoid this problem, say if there is  a string variable after another a string variable.


Title: "Open" Problem
Post by: Ralph on March 06, 2005, 12:50:15 AM
Just make sure you have a comma (,) immediately after each  string variable, as for this data file containing 5 digits and 2 strings:

1 2 3 4A, 5 6B, 7

For numbers, a space is OK, as is a comma.  So, this data could also be written as:

1,2,3,4A,5,6B,7

To write this file in QuickBASIC, you would use:

OPEN "Test1.txt" FOR OUTPUT AS #1
  PRINT #1, 1; 2; 3;"4A"; ","; 5; "6B"; ","; 7
CLOSE #1

which will write a file that looks like this:
1 2 3 4A , 5 6B , 7

As a matter of principal and consistancy, you might just write ALL your data files with comma separators, thus:

OPEN "Test1.txt" FOR OUTPUT AS #1
  PRINT #1, 1; ","; 2;","; 3;",";"4A"; ","; 5;","; "6B"; ","; 7
CLOSE #1

This will write a file that looks like this:

1 , 2 , 3 , 4A , 5 , 6B , 7


Title: "Open" Problem
Post by: Liquid Snake on March 06, 2005, 12:53:43 AM
OK, thanks for all the help.