1. Help in a Simple Program

This program was in Euphoric Mysteries book, I just added whatever to stop the program at the end. When I enter Euphoria as password when program runs it shows Access Denied. But when I change password=password[1..length(password)-2], then it works fine. Why???

object password, whatever 
 
puts(1,"Enter your password: ") 
password = gets(0) 
 
password = password[1..length(password)-1] 
 
if compare(password,"Euphoria")=0 then 
  puts(1,"[Password Authenticated]") 
else 
  puts(1,"[Incorrect password - Access Denied!]") 
end if 
 
whatever=gets(0) 

[edit: added eucode tags. --Matt]

new topic     » topic index » view message » categorize

2. Re: Help in a Simple Program

Euphoric_Kid said...

When I enter Euphoria as password when program runs it shows Access Denied. But when I change password=password[1..length(password)-2], then it works fine. Why???

There is a bug in Euphoria when it comes to reading console input in a Windows system. The line ending should be a single LF (#0A) character, but in Windows we are getting a CRLF (#0D#0A) combination. Try this instead.

object password, whatever 
 
puts(1,"Enter your password: ") 
password = gets(0) 
 
if password[$] = 10 then 
   password = password[1..$-1] 
end if 
if password[$] = 13 then 
   password = password[1..$-1] 
end if 
 
if compare(password,"Euphoria")=0 then 
  puts(1,"[Password Authenticated]") 
else 
  puts(1,"[Incorrect password - Access Denied!]") 
end if 
 
whatever=gets(0) 

If you are using Eu4, then a simplier way is ...

include std/text.e 
object password, whatever 
 
puts(1,"Enter your password: ") 
password = trim(gets(0)) 
 
if equal(password,"Euphoria") then 
  puts(1,"[Password Authenticated]") 
else 
  puts(1,"[Incorrect password - Access Denied!]") 
end if 
 
whatever=gets(0) 
new topic     » goto parent     » topic index » view message » categorize

3. Re: Help in a Simple Program

Euphoric_Kid said...

When I enter Euphoria as password when program runs it shows Access Denied. But when I change password=password[1..length(password)-2], then it works fine. Why???

To return from the gets(0) function, you have to press Enter, right? That Enter character is appended to whatever you typed, so your sequence looks like

"Euphoria\n"

Using the $-1 truncates the \n and gives you the pure "Euphoria."

You could also use:

   password = prompt_string( "Enter your password: " ) 

and not worry about it.

I think.

Oops: Didn't see you were using "$-2" and didn't know about the bug pointed out by DerekP. My solution still stands, however. smile

new topic     » goto parent     » topic index » view message » categorize

4. Re: Help in a Simple Program

prompt_string in Eu 4.0.3 doesn't work as the manual states, at least when used in Windows. It strips the #10 but not the #13.

new topic     » goto parent     » topic index » view message » categorize

5. Re: Help in a Simple Program

dr_can said...

prompt_string in Eu 4.0.3 doesn't work as the manual states, at least when used in Windows. It strips the #10 but not the #13.

This is a known bug and has already been fixed for the next release.

The problem is that under Windows, gets() returns a string terminated with CRLF rather than just a LF.

As a workaround for now, use the trim() function on the string returned by prompt_string().

include std/text.e 
include std/comsole.e 
 
sequence ans 
 
ans = trim(prompt_string("Question:")) 
new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu