Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Error in running program
#1
Hey guys. I got most of this code from this forum from last time I asked and this time I wanted to edit it so It suits my thoughts. For like users choosing there own characterset they want to use in making a password this is what I came up with.. Hopefully someone can tell me where my problem is Smile

Code:
REM Generate a random password.
START:
PRINT "Password Generator 1.0"
PRINT
PRINT "Charactersets available"
PRINT
PRINT "1) abcdefghijklmnopqrstuvwxyz "
PRINT
PRINT "2) ABCDEFGHIJKLMNOPQRSTUVWXYZ "
PRINT
PRINT "3) 0123456789!?><+_-*&^%$#@,./][\"
PRINT
PRINT "4) ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!?><+_-*&^%$#@,./][\ "

INPUT "Input your characterset"; selection

  IF selection = 1 THEN

  characterset = "abcdefghijklmnopqrstuvwxyz"
     GOTO PROGRAM

  ELSE IF selection = 2 THEN

     characterset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
     GOTO PROGRAM
  ELSE IF selection = 3 THEN

     characterset = "0123456789!?><+_-*&^%$#@,./][\"
     GOTO PROGRAM
  ELSE IF selection = 4 THEN

     characterset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!?><+_-*&^%$#@,./][\"
     GOTO PROGRAM
  ELSE
     PRINT "Invalid Selection"
     GOTO START
  END IF

PROGRAM:
CLS
DEFINT A-Z
RANDOMIZE TIMER

Chars$ = "characterset"
NChars = LEN(Chars$)'This allows later add or delete of characters

PRINT "Enter length of password desired (max=36) ";
INPUT plen
IF plen < 1 OR plen > 36 THEN PRINT "Invalid length": SYSTEM

pw$ = ""
randlower = 1
randupper = NChars
FOR x = 1 TO plen
   rand = INT(RND * (randupper - randlower + 1)) + randlower
   pw$ = pw$ + MID$(Chars$, rand, 1)'Pickup a char from random position in Chars$
NEXT x
PRINT "The random password is "; pw$

INPUT "Save as: ", sf$
OPEN sf$ FOR OUTPUT AS #1
WRITE #1, pw$
CLOSE #1

SYSTEM
img]http://img213.imageshack.us/img213/6104/sig1jb.gif[/img]
Reply
#2
Don't DEFINT A-Z and RANDOMIZE TIMER need to be at the beginning of the program?
In the beginning, there is darkness – the emptiness of a matrix waiting for the light. Then a single photon flares into existence. Then another. Soon, thousands more. Optronic pathways connect, subroutines emerge from the chaos, and a holographic consciousness is born." -The Doctor
Reply
#3
study this

Code:
REM Generate a random password.

DEFINT A-Z
RANDOMIZE TIMER

'' before you had it as a single.
dim characterset as string

do

  cls

  PRINT "Password Generator 1.0"
  PRINT
  PRINT "Charactersets available"
  PRINT
  PRINT "1) abcdefghijklmnopqrstuvwxyz "
  PRINT
  PRINT "2) ABCDEFGHIJKLMNOPQRSTUVWXYZ "
  PRINT
  PRINT "3) 0123456789!?><+_-*&^%$#@,./][\"
  PRINT
  PRINT "4) ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!?><+_-*&^%$#@,./][\ "
  
  INPUT "Input your characterset"; selection
  
  IF selection = 1 THEN

    characterset = "abcdefghijklmnopqrstuvwxyz"
    exit do

  ELSEIF selection = 2 THEN

    characterset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    exit do

  ELSEIF selection = 3 THEN

    characterset = "0123456789!?><+_-*&^%$#@,./][\"
    exit do

  ELSEIF selection = 4 THEN

    characterset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!?><+_-*&^%$#@,./][\"
    exit do

  END IF

loop
  
dim chars as string

chars = characterset
NChars = LEN(chars)'This allows later add or delete of characters

do

  CLS
  
  PRINT "Enter length of password desired (max=36) ";
  INPUT plen
  IF plen > 0 and plen < 37 THEN
    
    exit do
  
  end if
  
loop

dim pw as string

randlower = 1
randupper = NChars
FOR x = 1 TO plen
   rand = INT(RND * randupper) + 1
   pw = pw + MID$(Chars, rand, 1)'Pickup a char from random position in Chars$
NEXT x
PRINT "The random password is "; pw

INPUT "Save as: ", sf$
OPEN sf$ FOR OUTPUT AS #1
WRITE #1, pw
CLOSE #1

SYSTEM
Reply
#4
There is onething I dont understand in code which is.. EXIT DO.. What does this mean there are so many DO statements in code I dont know where it ends up going. Can somone please explain it. Also thanks for that cha0s It works now.. Thanks alot im going to do some minor work on it thanks man. Ill post up finished result
img]http://img213.imageshack.us/img213/6104/sig1jb.gif[/img]
Reply
#5
For some reason I wanted to make like a selection for user to specify if he wants to save file or not.. And for some reason it keeps printing redo from start.. This is the code hopefully someone can help me..

Code:
REM Generate a random password.

DEFINT A-Z
RANDOMIZE TIMER
START:

'' before you had it as a single.
DIM characterset AS STRING

DO

  CLS

  PRINT "Password Generator 1.0"
  PRINT
  PRINT "Charactersets available"
  PRINT
  PRINT "1) abcdefghijklmnopqrstuvwxyz "
  PRINT
  PRINT "2) ABCDEFGHIJKLMNOPQRSTUVWXYZ "
  PRINT
  PRINT "3) 0123456789!?><+_-*&^%$#@,./][\"
  PRINT
  PRINT "4) ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!?><+_-*&^%$#@,./][\ "
  PRINT
  PRINT
  INPUT "Input your characterset"; selection

  IF selection = 1 THEN

    characterset = "abcdefghijklmnopqrstuvwxyz"
    EXIT DO

  ELSEIF selection = 2 THEN

    characterset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    EXIT DO

  ELSEIF selection = 3 THEN

    characterset = "0123456789!?><+_-*&^%$#@,./][\"
    EXIT DO

  ELSEIF selection = 4 THEN

    characterset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!?><+_-*&^%$#@,./][\"
    EXIT DO

  END IF

LOOP

DIM Chars AS STRING

Chars = characterset
NChars = LEN(Chars)'This allows later add or delete of characters

DO

  CLS

  PRINT "Enter length of password desired (max=36) ";
  INPUT plen
  IF plen > 0 AND plen < 37 THEN
  
    EXIT DO

  END IF

LOOP

DIM pw AS STRING

randlower = 1
randupper = NChars
FOR x = 1 TO plen
   rand = INT(RND * randupper) + 1
   pw = pw + MID$(Chars, rand, 1)'Pickup a char from random position in Chars$
NEXT x
PRINT "The random password is "; pw
PRINT
PRINT
PASSWORD:
INPUT "Would you like to save password to file? (y/n)", save

SELECT CASE save
CASE y

PRINT
PRINT
INPUT "Save as: ", sf$
OPEN sf$ FOR OUTPUT AS #1
WRITE #1, pw
CLOSE #1


CASE n
GOTO START

CASE ELSE
PRINT "Invalid Selection"
GOTO PASSWORD

END SELECT

SYSTEM
img]http://img213.imageshack.us/img213/6104/sig1jb.gif[/img]
Reply
#6
you have to DIM save AS STRING, and check for CASE "y", CASE "n".
(Notice quotes)

Since you did DEFINT, and didn't specify save's type, it defaulted to integer, which can only take numbers. When you entered "y" or "n", it make you redo, expecting a number =)

oh btw, the EXIT DO makes it jump out of the DO...LOOP. DO LOOP's will loop forever if there are no conditions, in that case, the only way to "break out" of the loop is to enter a correct input, if you don't it just loops forever. You can also do things like this:


Code:
DIM exitcode AS INTEGER
DO

  REM Get input

  if input = good then exitcode = -1

LOOP WHILE exitcode = 0


get it?
Reply
#7
Oh Thanks man your a champ.. Now I understand it thanks alot
img]http://img213.imageshack.us/img213/6104/sig1jb.gif[/img]
Reply
#8
Here is complete program now. Thanks for all your help guys here is what I put together.

Code:
'This program is designed to generate passwords. Made by Ali Abdulsater and help from qbasicnews.com forum members.


DEFINT A-Z   'This sets the default data types for variables
RANDOMIZE TIMER   'Randomises the Timer
START: 'Anchor point

DIM characterset AS STRING 'This declares characterset as a STRING
DIM selection AS INTEGER   'This declares selection as a INTEGER


DO 'This is going to be sued to repeat a block of statements

  CLS ' Clear Screen

  PRINT "Password Generator 1.0 by Ali (Hybr!d)"
  PRINT
  PRINT "Charactersets available"
  PRINT
  PRINT "1) Lowercase Alphabet "
  PRINT
  PRINT "2) Uppercase Alphabet "
  PRINT
  PRINT "3) Numbers and Symbols"
  PRINT
  PRINT "4) Lowercase/Uppercase/Numbers and Symbols"
  PRINT
  PRINT "5) Exit Program"
  PRINT
  INPUT "Input your characterset (1/2/3/4) "; selection

             IF selection = 1 THEN

                   characterset = "abcdefghijklmnopqrstuvwxyz"
                   EXIT DO

            ELSEIF selection = 2 THEN

                   characterset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
                   EXIT DO

            ELSEIF selection = 3 THEN

                   characterset = "0123456789!?><+_-*&^%$#@,./][\"
                   EXIT DO

            ELSEIF selection = 4 THEN

                   characterset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!?><+_-*&^%$#@,./][\"
                   EXIT DO

            ELSEIF selection = 5 THEN
            
                   SYSTEM 'This is used to close all open files and return to Operating system


            ELSE
            
                  PRINT "Invalid Selection"
                  SLEEP 2
                  GOTO START

  END IF

LOOP

DIM Chars AS STRING 'This declares Chars as STRING

Chars = characterset
NChars = LEN(Chars) 'LEN function is used to return number of characters in a string

DO

  CLS

  PRINT "Enter length of password desired (max=36) ";
  INPUT plen
  IF plen > 0 AND plen < 37 THEN 'This checks if the length user input is within range
  
  EXIT DO

  END IF

LOOP

DIM pw AS STRING 'This declares pw as STRING

  randlower = 1
  randupper = NChars

             FOR x = 1 TO plen
  
             rand = INT(RND * randupper) + 1 'Produces random character
      
             pw = pw + MID$(Chars, rand, 1) 'Pickup a char from random position in Chars$
      
             NEXT x 'Goes for next x till user specified length is done
  
  PRINT "The random password is "; pw
  PRINT
  PRINT
PASSWORD: 'Anchor point

DIM save AS STRING 'This declares save as STRING
DIM selection1 AS STRING 'This declares selection1 as STRING


  INPUT "Would you like to save password to file? (y/n)", save
    
  SELECT CASE save 'Used for user to go alternative paths
  CASE "y"

  PRINT
  PRINT
  PRINT "Save in the following manner <filename>.txt"
  PRINT
  PRINT
  INPUT "Save as: ", sf$
  
             OPEN sf$ FOR OUTPUT AS #1
            
             WRITE #1, pw
            
             CLOSE #1
  PRINT
  PRINT "File saved!!"
  PRINT
  
ANOTHERPASS: 'Anchor Point

CLS

  INPUT "Would you like to make another password? (y/n)"; selection1
  
             IF selection1 = "y" THEN
  GOTO START

  ELSEIF selection1 = "n" THEN
  
  SYSTEM

  ELSE
  
  PRINT "Invalid Selection"
  
             SLEEP 1
            
  GOTO ANOTHERPASS
  
  END IF

  CASE "n"
  GOTO START

  CASE ELSE
  
  PRINT "Invalid Selection"
  
  GOTO PASSWORD

END SELECT

SYSTEM
img]http://img213.imageshack.us/img213/6104/sig1jb.gif[/img]
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)