Need Help with File I/O

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
SCC
Coder
Posts: 12
Joined: Mon Oct 09, 2006 9:53 pm

Need Help with File I/O

Post by SCC » Fri Dec 29, 2006 11:25 pm

So...i got to page 5 in the Qbasic BootCamp tut series, and i had a great idea for one of the little "assignments" at the end. It was a guess the number game, and i'm almost finished. But i ran into a problem...

I've been trying to get this right for about 3 hours straight now, and i just can't figure out how to get this code to work...I know what needs to be happening, but i can't find the right command.

I need to write 3 or more player's input numbers into a file, and at the same time, write the program's randomly generated number into a second file. After theyre written, i need them to be read back into the program and printed on the screen.

I have the code doing everything EXCEPT appending the numbers to the same line. Theyre appending like this:

1
2
3

They need to be doing this:

1 2 3

Anyways, here's the program (might need to change the location of the answer files...oh, and the answer files need to be deleted every time the program is run, or it will keep "appending" the numbers (any help on how to clear it after theyre inputed would be cool too lol))

Code: Select all

RANDOMIZE TIMER

Win = 0:Lost = 0
REM ------------------------------------------------------------------
OPEN "C:\mydocu~1\Answer1.txt" FOR APPEND AS #1
OPEN "C:\mydocu~1\Answer2.txt" FOR APPEND AS #2

Top:
LOCATE 3,4:COLOR 14:PRINT "WON  = ";Win
LOCATE 4,4:COLOR 14:PRINT "LOST = ";Lost

LOCATE 6,9:COLOR 9:PRINT "123 Number 456 Game 789"
LOCATE 7,5:COLOR 3:PRINT "Guess A Number Between 1 and 10"
LOCATE 8,5:COLOR 3:PRINT "Guess Right 3 Times and YOU WIN"

	IF Win = 3 THEN GOTO Win2
	IF Lost = 3 THEN GOTO Lost2

Answer = INT(RND * 10) + 1
	LOCATE 12,4:COLOR 9:INPUT Guess%

	WRITE #1, Guess%
	WRITE #2, Answer
			IF Guess% = Answer THEN GOTO Win1
			IF Guess% <> Answer THEN GOTO Lost1
REM ------------------------------------------------------------------
Win1:
LOCATE 10,4:COLOR 14:PRINT "YES! The Number Is "; Answer; "!!!"
	Win = Win + 1
		SLEEP 3:GOTO Top
REM ------------------------------------------------------------------
Lost1:
LOCATE 10,4:COLOR 4:PRINT "NO! The Number Was "; Answer; "!!!"
	Lost = Lost + 1
		SLEEP 3:GOTO Top
REM ------------------------------------------------------------------
Win2:
CLS:Win = 0:Lost = 0
LOCATE 3,4:COLOR 3:PRINT "YOU ARE THE CHAMPION!!!"
	SLEEP 3
	LOCATE 5,4:COLOR 7:PRINT "Play Again? Y/N"
	LOCATE 6,4:INPUT pa$
			IF UCASE$(pa$) = "Y" THEN GOTO top ELSE END
REM ------------------------------------------------------------------
Lost2:
CLOSE #1:CLOSE #2
CLS:Win = 0:Lost = 0
LOCATE 3,4:COLOR 3:PRINT "YOU LOST TOO MANY TIMES..."
	SLEEP 3
LOCATE 5,4:COLOR 7:PRINT "Your INPUT Answers Were..."
OPEN "C:\mydocu~1\Answer1.txt" FOR INPUT AS #1
	LOCATE 6,4:COLOR 4
	LINE INPUT #1, A1$
		PRINT A1$
			CLOSE #1

LOCATE 8,4:COLOR 7:PRINT "The Right Answers Were..."
OPEN "C:\mydocu~1\Answer2.txt" FOR INPUT AS #2
	LOCATE 9,4:COLOR 14
	LINE INPUT #2, A2$
		PRINT A2$
			CLOSE #2

		LOCATE 11,4:COLOR 7:PRINT "Play Again? Y/N"
		LOCATE 12,4:INPUT pa$
			IF UCASE$(pa$) = "Y" THEN GOTO top ELSE END

bungytheworm
Veteran
Posts: 288
Joined: Sat Feb 18, 2006 4:02 pm

Post by bungytheworm » Sat Dec 30, 2006 4:05 am

Cant acces to QB now but when youre writing numbers on file you need to do something like this

Code: Select all

WRITE #1, Number1, Number2, Number3
If you use append and start writing in file, it allways makes a new line in there. So basicly, you got to write whole line at once.

SCC
Coder
Posts: 12
Joined: Mon Oct 09, 2006 9:53 pm

Post by SCC » Sat Dec 30, 2006 3:37 pm

Well, i figured it out. Can't use files, so i just went ahead and stored the numbers in variables and printed them out that way. :\

Now i just need to clean it up, possibly make a few subs, and then i should have a decent lookin program. :)

Post Reply