Computer Science Canada

Password

Author:  ans4 [ Sat Oct 28, 2006 11:41 am ]
Post subject:  Password

I'm trying to write a program so that when you type something it shows up as asterisks like for a password and it tells you if you have it right or wrong. What I have so far works but only if you get it right on the first try. I think the problem has to do with the "word:= word + pass" but I can't figure it out.

Quote:
var password := "123"
var pass : string (1)
var word : string := ""

loop
locate (1, 1)
put "Password " ..
locate (1, 10)
loop
getch (pass)
exit when pass = KEY_ENTER
word := word + pass
put "*" ..
end loop
if word = password then
cls
put "Hello"
exit
else
locate (1, 30)
put "Wrong password"
end if
end loop

Author:  Ultrahex [ Sat Oct 28, 2006 11:58 am ]
Post subject: 

The Last Else you need to reset the password string


...

code:

    if word = password then
        cls
        put "Hello"
        exit
    else
        cls
        locate (1, 30)
        put "Wrong password"
        word := ""
    end if

Author:  richcash [ Sat Oct 28, 2006 11:59 am ]
Post subject: 

Yes, it does have to do with word := word + pass. If the user enters 12, it's wrong, but the 12 stays there, so when the enter 123, it's still wrong because now it's 12123! You need to reset word to "" when it's the wrong password.
code:
var password := "123"
var pass : string (1)
var word : string := ""

loop
    locate (1, 1)
    put "Password " ..
    locate (1, 10)
    loop
        getch (pass)
        exit when pass = KEY_ENTER
        word := word + pass
        put "*" ..
    end loop
    if word = password then
        cls
        put "Hello"
        exit
    else
        locate (1, 30)
        put "Wrong password"
        word := ""  %<-- Re-initialize word!
    end if
end loop

Author:  ans4 [ Tue Oct 31, 2006 8:27 pm ]
Post subject: 

Thanks for the help. Anyone know how I could make it so that I could backspace and erase the password?

Author:  Expirant [ Tue Oct 31, 2006 8:49 pm ]
Post subject: 

I suppose a method of making backspace take letters out could resemble something like this:

code:

if ord (pass) = 8 then
   word := word (1 .. length (word) - 1)
end if


That will make the string "word" lose its last character. How you show that on the screen I'm sure you can find out (as in, displaying the correct amount of asterix)

Hope that helps,
Expirant


: