Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Password
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
ans4




PostPosted: 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
Sponsor
Sponsor
Sponsor
sponsor
Ultrahex




PostPosted: Sat Oct 28, 2006 11:58 am   Post subject: (No 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
richcash




PostPosted: Sat Oct 28, 2006 11:59 am   Post subject: (No 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
ans4




PostPosted: Tue Oct 31, 2006 8:27 pm   Post subject: (No subject)

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




PostPosted: Tue Oct 31, 2006 8:49 pm   Post subject: (No 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
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: