AboutAlex Barry Expertise I have been a qbasic programmer since 2000, creating games, minor libraries and various small programs. I have experience using interrupts, graphics, file input/output, the mouse cursor, and using libraries. I have also learned FreeBASIC, c/c++, python, php and html.
I do not claim to be an absolute authority in any language, but I don't mind looking things up and learning with you.
Experience Hobby programming since 2000
I no longer belong to any community programming groups, but do have knowledge of *basic dialects and C/C++
Question QUESTION: Hi, I am trying to make a program which makes you input your name then uses open statement to make a .dat file but before the name of the .dat file is the name of your name for an example you type your name as Chad and it makes chad.dat. Although I am completely stuck on this.
Please help,
Chad
ANSWER: Hi, Chad,
This is a fairly common issue, but I can definately help you out.
In QBasic, anything surrounded in quotes is a string, whether it is in a command or a variable.
The open command has quotation marks:
open "file" for...
So, your code probably looks something like this:
DIM fname AS STRING
INPUT "Name: ", fname
' For the sake of universalism:
fname = LCASE$( fname )
That last line just makes the name all lower-case...this will come in handy if someone inputs their name as "cHAd" or something similar.
Now, for the open statement, which should be fairly easy:
OPEN fname + ".dat" FOR...
I hope that helps you out,
-Alex
---------- FOLLOW-UP ----------
QUESTION: Thanks Alex,
I have another problem if you dont mind helping. I made a simple register and login code. Which checks the name inputted to name in database and password inputted to password. But after I register if i close qbasic and attempt to login it does not work.
Here is a my code:
CLS
GOSUB request
END
request:
GOSUB main
GOSUB accept
DO WHILE number$ <> "3"
SELECT CASE number$
CASE IS = "1"
CLS
GOSUB add
CASE IS = "2"
CLS
GOSUB record
CASE IS = "3"
GOSUB think
END SELECT
GOSUB main
GOSUB accept
LOOP
RETURN
accept:
COLOR 14
LOCATE 7, 20: INPUT "Please Select Number"; number$
COLOR 7
DO WHILE number$ <> "1" AND number$ <> "2" AND number$ <> "3"
BEEP: BEEP
COLOR 12
LOCATE 12, 20: PRINT "Invalid"
COLOR 14
LOCATE 7, 41: PRINT " "
LOCATE 7, 42: INPUT "", number$
COLOR 7
LOOP
CLS
RETURN
record:
CLS
OPEN "ACC.dat" FOR INPUT AS #1
INPUT "What is your first name"; check$
INPUT "What is your last name"; check2$
INPUT "What is your email"; check3$
INPUT "What is your Bank ID"; check4$
IF check$ = first$ AND check2$ = Last$ AND check3$ = email$ AND check4$ = tot$ THEN
CLOSE #1
GOSUB in
ELSE
CLOSE #1
CLS
PRINT "Invalid"
SLEEP
GOSUB request
END IF
RETURN
add:
CLS
OPEN "Acc.dat" FOR APPEND AS #1
INPUT "First Name==>", first$
INPUT "Last Name ===>", Last$
INPUT "Email ===>", email$
IF LEFT$(first$, 1) = "A" THEN
totacc2$ = "23"
ELSEIF LEFT$(UCASE$(first$), 1) = "B" THEN
totacc2$ = "27"
ELSEIF LEFT$(UCASE$(first$), 1) = "C" THEN
totacc2$ = "21"
ELSEIF LEFT$(UCASE$(first$), 1) = "D" THEN
totacc2$ = "22"
ELSEIF LEFT$(UCASE$(first$), 1) = "E" THEN
totacc2$ = "28"
ELSEIF LEFT$(UCASE$(first$), 1) = "F" THEN
totacc2$ = "20"
ELSEIF LEFT$(UCASE$(first$), 1) = "G" THEN
totacc2$ = "3"
ELSEIF LEFT$(UCASE$(first$), 1) = "H" THEN
totacc2$ = "7"
ELSEIF LEFT$(UCASE$(first$), 1) = "I" THEN
totacc2$ = "62"
ELSEIF LEFT$(UCASE$(first$), 1) = "J" THEN
totacc2$ = "12"
ELSEIF LEFT$(UCASE$(first$), 1) = "K" THEN
totacc2$ = "81"
ELSEIF LEFT$(UCASE$(first$), 1) = "L" THEN
totacc2$ = "85"
ELSEIF LEFT$(UCASE$(first$), 1) = "M" THEN
totacc2$ = "39"
ELSEIF LEFT$(UCASE$(first$), 1) = "N" THEN
totacc2$ = "38"
ELSEIF LEFT$(UCASE$(first$), 1) = "O" THEN
totacc2$ = "33"
ELSEIF LEFT$(UCASE$(first$), 1) = "P" THEN
totacc2$ = "68"
ELSEIF LEFT$(UCASE$(first$), 1) = "Q" THEN
totacc2$ = "72"
ELSEIF LEFT$(UCASE$(first$), 1) = "R" THEN
totacc2$ = "92"
ELSEIF LEFT$(UCASE$(first$), 1) = "S" THEN
totacc2$ = "34"
ELSEIF LEFT$(UCASE$(first$), 1) = "T" THEN
totacc2$ = "74"
ELSEIF LEFT$(UCASE$(first$), 1) = "U" THEN
totacc2$ = "92"
ELSEIF LEFT$(UCASE$(first$), 1) = "V" THEN
totacc2$ = "43"
ELSEIF LEFT$(UCASE$(first$), 1) = "W" THEN
totacc2$ = "243"
ELSEIF LEFT$(UCASE$(first$), 1) = "X" THEN
totacc2$ = "143"
ELSEIF LEFT$(UCASE$(first$), 1) = "Y" THEN
totacc2$ = "313"
ELSEIF LEFT$(UCASE$(first$), 1) = "Z" THEN
totacc2$ = "203"
END IF
IF LEFT$(Last$, 2) = "A" THEN
totacc$ = "423"
ELSEIF LEFT$(UCASE$(Last$), 1) = "B" THEN
totacc$ = "24"
ELSEIF LEFT$(UCASE$(Last$), 1) = "C" THEN
totacc$ = "121"
ELSEIF LEFT$(UCASE$(Last$), 1) = "D" THEN
totacc$ = "90"
ELSEIF LEFT$(UCASE$(Last$), 1) = "E" THEN
totacc$ = "212"
ELSEIF LEFT$(UCASE$(Last$), 1) = "F" THEN
totacc$ = "210"
ELSEIF LEFT$(UCASE$(Last$), 1) = "G" THEN
totacc$ = "11"
ELSEIF LEFT$(UCASE$(Last$), 1) = "H" THEN
totacc$ = "75"
ELSEIF LEFT$(UCASE$(Last$), 1) = "I" THEN
totacc$ = "162"
ELSEIF LEFT$(UCASE$(Last$), 1) = "J" THEN
totacc$ = "12"
ELSEIF LEFT$(UCASE$(Last$), 1) = "K" THEN
totacc$ = "81"
ELSEIF LEFT$(UCASE$(Last$), 1) = "L" THEN
totacc$ = "85"
ELSEIF LEFT$(UCASE$(Last$), 1) = "M" THEN
totacc$ = "319"
ELSEIF LEFT$(UCASE$(Last$), 1) = "N" THEN
totacc$ = "382"
ELSEIF LEFT$(UCASE$(Last$), 1) = "O" THEN
totacc$ = "313"
ELSEIF LEFT$(UCASE$(Last$), 1) = "P" THEN
totacc$ = "6428"
ELSEIF LEFT$(UCASE$(Last$), 1) = "Q" THEN
totacc$ = "202"
ELSEIF LEFT$(UCASE$(Last$), 1) = "R" THEN
totacc$ = "995"
ELSEIF LEFT$(UCASE$(Last$), 1) = "S" THEN
totacc$ = "321"
ELSEIF LEFT$(UCASE$(Last$), 1) = "T" THEN
totacc$ = "704"
ELSEIF LEFT$(UCASE$(Last$), 1) = "U" THEN
totacc$ = "92"
ELSEIF LEFT$(UCASE$(Last$), 1) = "V" THEN
totacc$ = "143"
ELSEIF LEFT$(UCASE$(Last$), 1) = "W" THEN
totacc$ = "177"
ELSEIF LEFT$(UCASE$(Last$), 1) = "X" THEN
totacc$ = "953"
ELSEIF LEFT$(UCASE$(Last$), 1) = "Y" THEN
totacc$ = "12"
ELSEIF LEFT$(UCASE$(Last$), 1) = "Z" THEN
totacc$ = "343"
END IF
tot$ = totacc$ + totacc2$
PRINT "Your Bank Id is "; tot$
SLEEP 2
WRITE #1, first$, Last$, email$, tot$
PRINT
CLOSE #1
GOSUB request
RETURN
in:
GOSUB action
GOSUB go
DO WHILE answer$ <> "4"
SELECT CASE answer$
CASE IS = "1"
CLS
GOSUB withdrawl
CASE IS = "2"
CLS
GOSUB deposit
CASE IS = "3"
CLS
GOSUB transaction
END SELECT
GOSUB action
GOSUB go
LOOP
RETURN
action:
CLS
OPEN "acc.dat" FOR INPUT AS #1
LOCATE 5, 20: PRINT "Account of "; first$; " "; Last$
CLOSE #1
LOCATE 6, 20: PRINT "================================="
LOCATE 8, 20: PRINT "1) Withdrawl"
LOCATE 9, 20: PRINT "2) Deposit"
LOCATE 10, 20: PRINT "3) Transaction"
LOCATE 11, 20: PRINT "4) Exit"
RETURN
go:
COLOR 14
LOCATE 7, 20: INPUT "Please Select Number"; answer$
COLOR 7
DO WHILE answer$ <> "1" AND answer$ <> "2" AND answer$ <> "3"
BEEP: BEEP
COLOR 12
LOCATE 12, 20: PRINT "Invalid"
COLOR 14
LOCATE 7, 41: PRINT " "
LOCATE 7, 42: INPUT "", answer$
COLOR 7
LOOP
CLS
RETURN
withdrawl:
OPEN "ACC.dat" FOR INPUT AS #1
OPEN tot$ + ".dat" FOR APPEND AS #2
INPUT "How much would you like to withdrawl"; with
DAT$ = DATE$
amt = with - 100
WRITE #2, with, dep, amt, DAT$
CLOSE #1
CLOSE #2
GOSUB in
RETURN
deposit:
OPEN "ACC.dat" FOR INPUT AS #1
OPEN tot$ + ".dat" FOR APPEND AS #2
INPUT "How much would you like to deposit"; dep
DAT$ = DATE$
amt = dep + 100
WRITE #2, with, dep, amt, DAT$
CLOSE #1
CLOSE #2
GOSUB in
RETURN
transaction:
OPEN "ACC.dat" FOR INPUT AS #1
OPEN tot$ + ".dat" FOR INPUT AS #2
PRINT "Your Transaction"
PRINT "----------------"
PRINT "Date Withdrawl Deposit Total"
PRINT "-------------------------------------------------------"
p1$ = " ####.## ####.## #####.##"
DO WHILE NOT EOF(2)
INPUT #2, with, dep, amt, DAT$
PRINT USING p1$; DAT$; with; dep; amt
LOOP
CLOSE #1
CLOSE #2
SLEEP 2
GOSUB in
RETURN
think:
LOCATE 10, 30: PRINT "Goodbye!"
RETURN
Answer Hello, again, Chad,
I think you may benefit from some code I just posted to answer another person's question:
To do this sort of program, you'll need to do a lot of work on your code. First, I'm going to give you a quick introduction to User Defined Types (UDTs), so my code makes sense.
A UDT is a variable type with customized information. So, instead of INTEGER, STRING, or other data types, a UDT have have multiple data for one variable:
TYPE MyFirstUDT
hello AS INTEGER
world AS STRING * 25
END TYPE
If we were to make that a variable:
DIM myUDTvariable AS MyFirstUDT
And to assign values:
myUDTvariable.hello = 5
myUDTvariable.world = "Wow, this is a UDT!"
You'll notice that when I made a string, I put a "* 25" after it. Strings in UDTs need to have a definitive length so QBasic knows how big the variable is in bytes. You can make the string size any positive integer value, but it is necessary.
Here is how I would set your program up:
TYPE UserAccount
' You could add first and last name in here if you desired
' As far as how big the string is for that, it doesn't matter
' I usually stick with powers of 2, but that's just my weird preference
username as string * 64
domain as string * 32
password as string * 128
END TYPE
DECLARE SUB Resize( array() AS UserAccount, size AS INTEGER )
DECLARE FUNCTION LoadInformation%( file AS STRING )
DECLARE SUB UpdateInformation( file AS STRING )
DECLARE FUNCTION NumAccounts%()
DECLARE FUNCTION FindAccount%( username AS STRING )
DECLARE FUNCTION NewAccount%( username AS STRING, password AS STRING )
DIM SHARED domain AS STRING
REDIM SHARED account(0) AS UserAccount
' This can be changed to whatever you want...this would be after the @ symbol
domain = "yahoo.com"
SUB Resize( array() AS UserAccount, size AS INTEGER )
DIM tmp( LBOUND( array ) TO UBOUND( array ) ) AS UserAccount
DIM i AS INTEGER
FOR i = LBOUND( array ) TO UBOUND( array )
tmp( i ) = array( i )
NEXT i
IF size = 0 THEN
REDIM array( 0 ) AS UserAccount
EXIT SUB
END iF
REDIM array( 1 TO size ) AS UserAccount
FOR i = 1 TO size
array( i ) = tmp( i )
NEXT i
END SUB
FUNCTION LoadInformation%( file AS STRING )
LoadInformation% = 0
DIM handle AS INTEGER
DIM size AS INTEGER
DIM num AS INTEGER
DIM tmp AS UserAccount
DIM i AS INTEGER
size = LEN( tmp )
handle = FREEFILE()
OPEN file FOR RANDOM AS #handle LEN=size
num = LOF( handle ) / size
IF num = 0 THEN GOTO loadfinish
REDIM account( 1 TO num ) AS
i = 1
DO UNTIL EOF( handle )
GET #handle, , account( i )
i = i + 1
LOOP
LoadInformation% = 1
loadfinish:
CLOSE #handle
END FUNCTION
SUB UpdateInformation( file AS STRING )
IF NumAccounts%() = 0 THEN EXIT FUNCTION
DIM handle AS INTEGER
DIM size AS INTEGER
DIM tmp AS UserAccount
DIM i AS INTEGER
size = LEN( tmp )
handle = FREEFILE()
KILL file
OPEN file FOR RANDOM AS #handle LEN=size
FOR i = LBOUND( account ) TO UBOUND( account )
PUT #handle, , account( i )
i = i + 1
NEXT i
CLOSE #handle
END SUB
FUNCTION NumAccounts%()
NumAccounts% = UBOUND( account )
END FUNCTION
FUNCTION FindAccount%( username AS STRING )
FindAccount% = 0
IF NumAccounts%() = 0 THEN EXIT FUNCTION
DIM i AS INTEGER
FOR i = LBOUND( account ) to UBOUND( account )
IF username = LTRIM$( RTRIM$( account( i ).username ) ) ) THEN
FindAccount% = i
Exit Function
END IF
NEXT i
END FUNCTION
FUNCTION NewAccount%( username AS STRING, password AS STRING )
NewAccount% = 0
IF FindAccount%( username ) <> 0 THEN EXIT FUNCTION
Resize( account(), NumAccounts%() + 1 )
NewAccount% = UBOUND( accounts )
account( NewAccount% ).username = username
account( NewAccount% ).password = password
account( NewAccount% ).domain = domain
END FUNCTION
All my functions in this code, if they return 0, then an error of some sort has occured (if the file doesn't have account information, if a username is already in use, etc.)
This is how I would setup a user-account system in QBasic. I suggest redoing your code to be procedure oriented (SUB/FUNCTIONs instead of GOTO and GOSUBs). The NewAccount() function creates a new account, and if the username already exists, then it returns 0.
You may have to change some information in the UserAccount UDT, and the Find and New account functions, but it shouldn't be too hard.
That should let you do new accounts, and they will be accessible each time you run the program.