|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
|
|
#1
|
||||
|
||||
|
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?
__________________
IE QUOTE | PHP Manual | Google | C/C++ Compiler | Linux Tutorials | General Stuff Game Dev |
|
#2
|
||||
|
||||
|
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 05:38 PM. |
|
#3
|
||||
|
||||
|
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. |
|
#4
|
||||
|
||||
|
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?
|
|
#5
|
||||
|
||||
|
Ahh I see what you mean now.. I'm retrieving symbol and not an actual string.
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > Other Programming Languages > Comparing strings (scheme) |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|