Qbasicnews.com

Full Version: Help with a database... type... thing...
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm attempting to make a program that will let me enter sets of spanish and english words. In it, I have been trying to make it so you can enter the sets outside of the program and have them stored in the file Dict.dat.

The problem comes after I enter the information and it is supposedly stored in the file. In the section of code labeled "Problem Section." No matter what I change I always seem to get the message: "Input past end of file" and it highlights the line "INPUT #1, WordN$(i% + 1000)" and if I take this line out, it simply moves to the next line up.

here is the program up to the problem section;

Code:
LET number% = 1
DIM WordN$(1 to 2000)
IF number% < 2 THEN LET number% = 2
origin: CLS
CLOSE #1
OPEN "Dict.dat" FOR OUTPUT AS #1
     DO UNTIL WordN$(number% - 1) = "exit"
          INPUT "English: ", WordN$(number%)
          INPUT "Spanish: ", WordN$(number% + 1000)
          WRITE #1, WordN$(number%), WordN$(number% + 1000)
          PRINT ""
          number% = number% + 1
     LOOP
     WRITE #1, number%
CLOSE #1

'problem section
LET i% = 1
OPEN "Dict.dat" FOR INPUT AS #1
     FOR i% = i% TO number%
          INPUT #1, WordN$(i%)
          INPUT #1, WordN$(i% + 1000)
     NEXT i%
     INPUT #1, number%
CLOSE #1

thanks for any help!
Your code is:
Code:
'problem section
LET i% = 1
OPEN "Dict.dat" FOR INPUT AS #1
     FOR i% = i% TO number%
          INPUT #1, WordN$(i%)
          INPUT #1, WordN$(i% + 1000)
     NEXT i%
     INPUT #1, number%
CLOSE #1
1. Change your FOR to FOR i% = 1 to 2000
2. Just before your NEXT i%, enter IF EOF(1) THEN EXIT FOR

Anonymous

also, every time you open FOR OUTPUT, the file is erased and only what you output is saved. if you want to preserve the file, and only add the new contents onto the end, open it FOR APPEND
I have changed what Ralph said and here is the new code:
Code:
LET i% = 1
OPEN "Dict.dat" FOR INPUT AS #1
     FOR i% = 1 TO 2000
          INPUT #1, WordN$(i%)
          INPUT #1, WordN$(i% + 1000)
     IF EOF(1) THEN EXIT FOR
     NEXT i%
     INPUT #1, number%
CLOSE #1

Unfortunately, I get the same error message in the "INPUT #1, number%" line. If I remove this line, the highlighted error section jumps to the first INPUT. This is so agrivating! Please help!

P.S. I also changed the FOR OUTPUT to FOR APPEND, thanks cha0s!
EclipseOTO,

Here's your program. I'm going to intersperse my comments using REM statements. Please read them and reply with comments or questions.
Code:
LET number% = 1
DIM WordN$(1 to 2000)

REM YOU HAVE ENGLISH WORDS IN ARRAY POSITIONS 1 TO 1000 AND SPANISH IN SAME ARRAY FROM 1001 TO 2000.
REM IT WOULD BE EASIER IF YOU JUST HAD 2 SEPARATE ARRAYS.

IF number% < 2 THEN LET number% = 2

REM ABOVE LINE MAKES NO SENSE. OF COURSE NUMBER WILL BE <2, BECAUSE YOU JUST SET IT TO 1.
REM SO, REMOVE THIS LINE.

origin: CLS
CLOSE #1
OPEN "Dict.dat" FOR APPEND AS #1
     DO UNTIL WordN$(number% - 1) = "exit"
          INPUT "English: ", WordN$(number%)
          INPUT "Spanish: ", WordN$(number% + 1000)
          WRITE #1, WordN$(number%), WordN$(number% + 1000)
          PRINT ""
          number% = number% + 1
     LOOP
     WRITE #1, number%
REM I DON'T UNDERSTAND WHY YOU WRITE THE NUMBER OUT TO A RECORD ALL BY ITSELF.
REM YOU DON'T NEED THIS WRITE.
REM THE NUMBER ON THE FILE DOES NOT HELP YOUR PROGRAM.
CLOSE #1

REM WHY BOTHER READING THE ENGLISH AND SPANISH INTO WORDN$ ARRAY, WHEN ALL YOU ARE GOING TO DO WITH THEM IS WRITE THEM OUT?
REM I SUGGEST THE FOLLOWING:
          INPUT "English: ", EngWord$
          INPUT "Spanish: ", SpaWord$
          WRITE #1, EngWord$,SpaWord$
          PRINT ""
REM YOU DON'T NEED NUMBER% ANYMORE AS AN INDEX, SO I REMOVED THE LINE INCREMENTING IT BY ONE.
     LOOP


'problem section
LET i% = 1
OPEN "Dict.dat" FOR INPUT AS #1
     FOR i% = i% TO 2000
          INPUT #1, WordN$(i%)
          INPUT #1, WordN$(i% + 1000)

REM THIS IS PROBABLY YOUR BIG PROBLEM. YOU WROTE THE ENGLISH AND SPANISH ONTO THE SAME RECORD,
REM SO YOU HAVE TO READ THEM IN FROM THE SAME RECORD, LIKE THIS:
          INPUT #1, WordN$(i%) , WordN$(i% + 1000)

          IF EOF(1) THEN EXIT FOR
     NEXT i%
     INPUT #1, number%
REM WE ALREADY KNOW THAT YOU DON'T NEED THIS NUMBER.
REM IT SHOULD NOT BE ON THE FILE ANYMORE. (SEE ABOVE)
REM SO, REMOVE THIS INPUT STATEMENT.

CLOSE #1

EDIT:
This editing post was made 4 hours later. I am now making some corrections mostly having to do with the range of numbers from 1 to 1000 and 1001 to 2000. So, if you've read this post during the first 4 hours, please read it again.
*****
EclipseOTO,

I was thinking about your program overnight, and came up with some additional comments.

Here is your code after my changes made yesterday:
Code:
LET number% = 1
DIM WordN$(1 to 2000)
origin: CLS
CLOSE #1
OPEN "Dict.dat" FOR APPEND AS #1
     DO UNTIL WordN$(number% - 1) = "exit"
          INPUT "English: ", EngWord$
          INPUT "Spanish: ", SpaWord$
          WRITE #1, EngWord$,SpaWord$
          PRINT ""
     LOOP
     WRITE #1, number%
CLOSE #1

'problem section
LET i% = 1
OPEN "Dict.dat" FOR INPUT AS #1
     FOR i% = i% TO 2000
          INPUT #1, WordN$(i%) , WordN$(i% + 1000)
          IF EOF(1) THEN EXIT FOR
     NEXT i%
CLOSE #1


1) The check for "exit", is too late, you already stored the word "exit" onto the file. If you have several input runs, you will have the word "exit" several times in your file. So, I'm going to modify it to test for "exit" just at the time when the English word is input. You might want a better word for "exit", since then the word "exit" cannot be used. Maybe something like "/exit".

2) Since you now APPEND input to the file, and your program can have several input runs, then we somehow need to make sure that the user did not input more than a total 1000 sets of English or Spanish words, because that's the limit of the WordN$ array. To check this on every run, you would need logic to determine if the file exists, which can get messy. So, you're going to have to check the file manually to make sure the 1000 records are not exceeded. You can do this with Notepad for now.
Actually, 1000 English/Spanish words is a lot. Depending on the size of these words, you may find out that they might not fit into the array. This whole design is not very bulletproof.

3) You should also make sure that the English and Spanish words are not spaces or null. I'll add code to do this.

4) You should decide if you want the words to be in upper or lower case. Think about it. The way it is now you will store the words in whatever case the user keys them in.

5) I see a fundamental problem of duplicates. If the user enters the same English words more than once, you will have duplicates on your file. I don't know how critical this is for you.

6) Originally, you had the following statement:
FOR i% = i% TO Number%
and you later changed it to:
FOR i% = i% TO 2000

Neither is going to work. In this FOR loop you are reading the words from the file and storing them into the WordN$ array. It is the number of word records on the file which determines how many you need to read. So, I'll change this to only check the file for EOF and not by any FOR loop count.

Here's the code corrected for the above issues:
Code:
LET number% = 1        'This is not needed anymore
DIM WordN$(1 to 2000)
origin: CLS
CLOSE #1

OPEN "Dict.dat" FOR APPEND AS #1
     DO
        lang$ = "English: " : gosub getword
        if lcase$(Word$) = "exit" then EXIT DO
        EngWord$ = Word$
        lang$ = "Spanish: " : gosub getword
        SpaWord$=Word$
        WRITE #1, EngWord$,SpaWord$
        PRINT ""
     LOOP
CLOSE #1

*************************

getword:
  INPUT lang$ , Word$
  word$ = ltrim$(rtrim$(Word$))   'Strip leading or trailing blanks
  if Word$ = "" then
     print "ERROR: Blanks only or null are not allowed."
     print
     goto getword
  end if
return

***********************

'problem section
LET i% = 1
OPEN "Dict.dat" FOR INPUT AS #1
     DO WHILE NOT EOF(1)
        INPUT #1, WordN$(i%) , WordN$(i% + 1000)
        i% = i% + 1
     LOOP
CLOSE #1
*****
Thanks for all of your help! After reading Moneo's first reply I decided that I should probably scrap the program and start from scratch, since the program was still very small. After doing this I decided to replace the spanish-english word system with one consisting of two different variables. This helps lots and now works perfectly. I learned a lot, thanks for your help, if you want to see the end result of the program, which I kind of doubt you would, send me a PM.

Thanks for all your help!
EclipseOTO