program help (if you have a free sec)

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
Agent_Firestalker
Coder
Posts: 18
Joined: Fri Feb 11, 2005 11:51 am
Location: Lea Monde Ruins

program help (if you have a free sec)

Post by Agent_Firestalker »

I'm trying to make a program that will help me memorize bible verses, but it's got a problem. (could be my lousdy programming. If anyone reading has a sec, read on...

It randomly picks a verse from the file verses.txt (see bottom of post for txt sample) and then asks you for the reference. It lowercases the refernce and compares it to the reference (ref's follow verses in txt file)
You must get three right to close the program.

(On a side note, if I put this in my startup folder and have three wrong guess cause a logoff, can QB run XP's logoff and shutdown commands?)


RANDOMIZE TIMER

'open file
OPEN "c:\qbasic\verses.txt" FOR INPUT AS #1

'clear variable "count"
count1 = 0
count2 = 0

start: CLS
'randomly determine which line of file to read
num = INT(RND * 3) + 1

'skips through file to specified line
FOR count = 1 TO num
INPUT #1, verse$
INPUT #1, reference$
NEXT count

'the verse display and user prompt
PRINT verse$
PRINT
INPUT " Reference?: "; guess$

'if verse # selected (count) is same as last one/two used, get another number
'(start over)
IF count = used1 OR count = used2 THEN GOTO start

'stores used verses so no verse is used twice
IF count1 = 0 THEN count = used1
IF count1 <> 0 AND count2 = 0 THEN count = used2

'checking guess with reference
guess$ = LCASE$(guess$)

IF guess$ = reference$ THEN PRINT : PRINT "Correct!": SLEEP 2: correctnum = correctnum+1
IF guess$ <> reference$ THEN PRINT : PRINT "wrong": GOTO start
IF correctnum = 3 THEN PRINT "Nice job!"
IF correctnum < 3 THEN GOTO start

verses.txt (sample, verses aren't real)
everyone has sinned, rom 2:20
Jesus loves you, matt 12:1
pray often, john 14:1
in the beginning, gen 1:1


If anything is unclear, post your questions.
Thanks for your time,
Agent_Firestalker
"I ask you, is this a job for intelligent men?"
"Well show me one, i'll ask him?"

Val and Earl - "Tremors"
Dav
Coder
Posts: 14
Joined: Thu Jul 15, 2004 9:23 am
Contact:

close/re-open file

Post by Dav »

The error INPUT PAST END OF FILE occurs because the file pointer has already read through the verses.txt file, and you're letting INPUT keep reading the file without pointing it back to the beginning of the verses.txt file.

There are several ways to fix the code. Probably the easiest way without re-writing a bunch is to just add " SEEK #1, 1 " after the FOR/NEXT that steps through all the lines. That will put the file pointer to the beginning of the OPENed file, so when INPUT is called again it will be back at the beginning of the verses.txt file.

- Dav
Agent_Firestalker
Coder
Posts: 18
Joined: Fri Feb 11, 2005 11:51 am
Location: Lea Monde Ruins

Post by Agent_Firestalker »

Commands you never remember... SEEK.

Thanks, Dav
"I ask you, is this a job for intelligent men?"
"Well show me one, i'll ask him?"

Val and Earl - "Tremors"
Dav
Coder
Posts: 14
Joined: Thu Jul 15, 2004 9:23 am
Contact:

Post by Dav »

Glad to help. (Heh... 'SEEK and ye shall find' :wink: ).
Visit Dav's Qbasic Site at: www.qbasicnews.com/dav
Ralph
Veteran
Posts: 148
Joined: Fri Feb 09, 2007 3:10 pm
Location: Katy, Texas

Post by Ralph »

Yes, Dav, but Agent_Firestalker used the other one, Ask and thou shalt receive. And, it worked for the Agent! Ha, ha, couldn't pass that one up. :)
Ralph, with QuickBASIC 4.5, operating under Windows XP, wiht anHP LaserJet 4L Printer. Bilingual in English/Spanish
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

I dunno

Post by burger2227 »

I think it is best to just close the file when you are done with it. Then reopen it when you need to read it again. You could make a SUB program with the OPEN and CLOSE and your loop.

It is important that you not pass the End Of File. I usually use:

DO WHILE NOT(EOF(1))

You could also use: IF NOT(EOF(1)) THEN INPUT #1, verse$

Ted
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
User avatar
Seb McClouth
Veteran
Posts: 342
Joined: Wed Nov 09, 2005 7:47 am
Location: Inside the Matrix...
Contact:

Post by Seb McClouth »

Erm it could be me but wouldn't
FOR count = 1 TO num
INPUT #1, verse$
INPUT #1, reference$
NEXT count
resolve into a problem since in the txt-file, it's written like
everyone has sinned, rom 2:20
Wouldn't it have to be sumfin like

Code: Select all

FOR count = 1 TO num 
INPUT #1, verse$, reference$ 
NEXT count
grtz
QBinux is a Linux distribution with the aim of integrating the work of the vast community of free software developers at Pete's QBASIC Site in order to create a modern, performant, safe and easy to use system for system administrators and desktop users.
Agent_Firestalker
Coder
Posts: 18
Joined: Fri Feb 11, 2005 11:51 am
Location: Lea Monde Ruins

Post by Agent_Firestalker »

I thought i tried that, seb.. but I guess i've been working on XHTML too long lately.

Thanks all for your posts. It works now.
I just have to test it now by putting it in startup. (I could make it user specific, but since i'm the only user on my machine, startup works.)

Rock on...
Agent_Firestalker
"I ask you, is this a job for intelligent men?"
"Well show me one, i'll ask him?"

Val and Earl - "Tremors"
User avatar
Seb McClouth
Veteran
Posts: 342
Joined: Wed Nov 09, 2005 7:47 am
Location: Inside the Matrix...
Contact:

Post by Seb McClouth »

I'm sure there's a way to log off even shutdown the machine if you got the answers wrong. Just look around with google.

In Linux this'd be easy

Code: Select all

shutdown -h now
.

grtz
QBinux is a Linux distribution with the aim of integrating the work of the vast community of free software developers at Pete's QBASIC Site in order to create a modern, performant, safe and easy to use system for system administrators and desktop users.
Post Reply