Reading and checking

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
izidor
Veteran
Posts: 110
Joined: Wed Apr 22, 2009 3:13 am
Contact:

Reading and checking

Post by izidor »

I am working on a game in witch you must create account (with name and password)
Name is saved in n001.dat, password is saved in p001.dat.
and this is my problem: I want to make my program read n001.dat and p001.dat, and is the name and password that you wrote same as n001.dat and p001.dat

Code: Select all

input "-", startplay$
if startplay$ = "start game" then
input "Name: ",name$
input "Password: ",pass$
open "n001.dat" for output as #1
print #1, name$
close #1
open "p001.dat" for output as #1
print #1, pass$
close #1

end if

if startplay$ = "play game" then
input "Name: ",playname$
end if
if playname$ = name$ then
input "Password: ", playpass$
end if
if playpass$ = pass$ then
end if
User avatar
Mentat
Veteran
Posts: 409
Joined: Tue Aug 07, 2007 3:39 pm
Location: NC, US

Post by Mentat »

Hrm. Were you looking for this? I think you needed to load the Name$ and pass$ from the file.

Code: Select all

input "-", startplay$
if startplay$ = "start game" then
     input "Name: ",name$
     input "Password: ",pass$
     open "n001.dat" for output as #1
     print #1, name$
     close #1
     open "p001.dat" for output as #1
     print #1, pass$
     close #1
end if

if startplay$ = "play game" then
     input "Name: ",playname$
' Removed "end if"

     'Reading password and name from file
     open "p001.dat" for input as #1
     input #1, pass$
     close #1
     open "n001.dat" for input as #1
     input #1, name$
     close #1
     
     if playname$ = name$ then
          input "Password: ", playpass$
     end if
     if playpass$ = pass$ then
          'Something goes here
     end if

'Put New end if here
end if 
Sorry if there are any code errors above. I have no way of checking if it works.
For any grievances posted above, I blame whoever is in charge . . .
izidor
Veteran
Posts: 110
Joined: Wed Apr 22, 2009 3:13 am
Contact:

Post by izidor »

thx for reply but it says "C++ compilation failed"
but thx anyway
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 »

Why not just have a user enter an alias and search a file for that name?

If the name is found, the file also holds their password. Just verify it next.

If the name is not found, then ask for a password. Then start the game.

1) Who is gonna want to type "Start game" to play?
2) All you need is to APPEND a file if necesssary so that you can have more than one player's data. OUTPUT erases a file!

To verify that a data file exists just open for APPEND at the start of the program. This will make sure it exists.

Code: Select all

OPEN "Mydata.dat" FOR APPEND AS #1  'creates file if not found
     IF LOF(1) THEN exists = 1    'checks for data in file
CLOSE #1
INPUT "Enter your user name: ", name$
IF exists THEN 
   OPEN "Mydata.dat" FOR INPUT AS #1 'look for the name in file
   DO WHILE NOT EOF(1)
       INPUT #1, user$, pass$
       IF name$ = user$ THEN found = 1: EXIT DO
   LOOP
   CLOSE #1
END IF
IF found THEN 
    password$ = pass$
    INPUT "Enter your password: ", pw$
    IF pw$ = password$ THEN GOSUB Startgame ELSE SYSTEM
ELSE: INPUT "Enter a new password: ", passw$
    OPEN "Mydata.dat" FOR APPEND as # 1
      WRITE #1, name$, passw$
    CLOSE #1
    GOSUB Startgame
END IF
 
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
Mentat
Veteran
Posts: 409
Joined: Tue Aug 07, 2007 3:39 pm
Location: NC, US

Post by Mentat »

izidor wrote:thx for reply but it says "C++ compilation failed"
but thx anyway
What software are you using for running your code? C++ is a whole different language.
For any grievances posted above, I blame whoever is in charge . . .
izidor
Veteran
Posts: 110
Joined: Wed Apr 22, 2009 3:13 am
Contact:

Post by izidor »

@Mentant
There wasnt problem in your code, it was in my

@burger2227
Sorry but Mentant's code is better to me because its more simple :)

@Mentant

I'm using QB64, yeah its strange to me too but i think it compiles bas file to exe with some c++ app or something else
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 »

Programs are not made to be EASY for the programmer! They should be easy for the USER.

Using OPEN FOR OUTPUT allows only ONE data entry. That is not only simple, but stupid!

That makes any user overwrite another's data! So why even use a file?

Just make them use some hidden password you want LOL.......That is a game all by itself!
Last edited by burger2227 on Fri Apr 24, 2009 12:05 am, edited 1 time in total.
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
MystikShadows
Veteran
Posts: 703
Joined: Sun Nov 14, 2004 7:36 am
Contact:

Post by MystikShadows »

burger2227 wrote:Programs are not made to be EASY for the programmer! They should be easy for the USER.
Words of wisdom right there. :)
When God created light, so too was born, the first Shadow!

MystikShadows

Need hosting? http://www.jc-hosting.net

Interested in Text & ASCII development? Look no further!
http://www.ascii-world.com
Post Reply