#1
  1. Only the strong survives!!.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Feb 2003
    Location
    A World of wonders.
    Posts
    5,583
    Rep Power
    407

    Comparing strings (scheme)


    For some reason i keep getting Access Denied in this little snippet

    Code:
    (display "Please enter your Username: ") ;Display Username Message
     (define n (read)) (newline) ;Read input from buffer
     
    (display "Please enter your Password: ") ;Display Password Message
     (define p (read)) (newline) ;Read input from buffer
    
     (if (eq? p "a123456") ;Check to make sure that the password is correct
      (display "Access Granted") ;Grant access
      (display "Access Denied"))
    Anyone knows why?
  2. #2
  3. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Here is my solution:

    Code:
    (define password 'a123456)
    
    (define (ask-username)
            (display "Account: ")
    	(read))
    
    (define (ask-password)
    	(display "Password: ")
    	(if (equal? password (read))
    	    "Granted!"
    	    "Denied!"))
    
    (define (get-info)
            (let ((pas (ask-password))
    	      (acc (ask-username)))
    	     '(acc pas)))
    (ask-username) stores whatever the user enters in after Account: into the variable acc.
    (ask-password) stores whatever the user enters in after Password: into the variable pas.
    (get-info) creates the two variables (acc and pas) and returns a list containing both items.

    This is just one solution.
    Last edited by Yegg`; June 13th, 2006 at 06:38 PM.
  4. #3
  5. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    This works. The problem is that (read) returns lisp code not a string so you need to compare symbols .

    Code:
    (display "Please enter your Username: ") ;Display Username Message
    (define n (read))
    
    (newline)
     
    (display "Please enter your Password: ") ;Display Password Message
    (define p (read))
    
    (newline)
    
    (display (if (eq? p 'a123456)
                 "Access Granted"
                 "Access Denied"))
    If you want to get input as a string then you have to write your own little function to read until a newline is given . The advantage of this is that you have more control over the language: if you don't want the newline character on the end then you wont get it and hence don't have to strip it.

    Hope this helps man,

    Mark.

    Comments on this post

    • LinuxPenguin agrees : Great answer, mark
    programming language development: www.netytan.com Hula

  6. #4
  7. Only the strong survives!!.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Feb 2003
    Location
    A World of wonders.
    Posts
    5,583
    Rep Power
    407
    By adding "'" worked fine.. but what am i doing here? why do i have to add '? doesnt that create a newline if i used it with display?
  8. #5
  9. Only the strong survives!!.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Feb 2003
    Location
    A World of wonders.
    Posts
    5,583
    Rep Power
    407
    Ahh I see what you mean now.. I'm retrieving symbol and not an actual string.

IMN logo majestic logo threadwatch logo seochat tools logo