Thread: How to hide what a user has entered with *'s

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    7

    Question How to hide what a user has entered with *'s

    Hi,

    Im pretty new to programming, and couldnt find how to do this anywhere.

    Im looking for some code so that when a user enters a 4 digit number or something else, each digit is hidden behind a ****, with the variable still being what the user entered.

    Apologies if this is very basic, but we all needed help at some point

    Thanks in advance, Stuart

    ADDED:

    Also, one more question, how would i make sure that the user has entered it as a 4 digit number, instead of using

    Code:
    variable > 1000 || variable <= 10000
    as this doesnt allow numbers begginning with a '0'.

    Thanks again, Stuart
    Last edited by stuartg123; 04-15-2007 at 11:48 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Hiding input depends on your OS and compiler, there is no standard way to do such a thing.

    As for the other question, read the input as a string, make sure there are 4 characters, then check each one is a digit.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    The 'standard' (not really standard, but the most common way) way of solving your first question is to do something like this:

    Code:
    #include <conio.h> // Not a standardised header
    #include <iostream>
    #include <string>
    
    int main( void )
    {
        std::string str;
        std::cout<< "Enter something: ";
    
        while ( (str += getch()).at(str.size()) != VK_RETURN )
            std::cout<< "*";
    
        std::cout<< std::endl << str << std::endl;
    
        return 0;
    }
    That while loop is kinda condensed, but you could break it up if you wanted to.

    edit: This is very basic. You'd probably want to do lots of testing to see if someone hit backspace etc, and if you do that here it'll keep on adding *'s regardless. If you want to get a number out of this you should use stringstreams to accomplish it.
    Last edited by twomers; 04-15-2007 at 12:57 PM. Reason: no <string>

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    7
    Hello,

    Thanks for the replies.

    Salem, thanks for the help. I can now not put in a 5 or more digit number, if I do, the loop will run and not stop giving chance to enter a new number. Also, if I enter a 4 digit number, then another 4 digit number that is the same, and check that the second number that the user enters is the same as the first number, it will say that they do not match, whereas it didnt when using 'int'. The same happens with 3, 2 or 1 digit numbers. Please could you see if anything is wrong within this code?

    Code:
    #include <iostream>
    using namespace std;
    
    int main ()
    {
        int loop;
        do//set pin loops
        {
             char pin[5];
             char pinconfirm[5];
             loop = 1;
             cout<< "Please set 4 digit pin code: ";//set pin
             cin.getline ( pin, 5, '\n' ); //enter pin
             cout<< "\n\nPlease confirm: ";//confirm pin
             cin.getline ( pinconfirm, 5, '\n' );//enter confirmation
             if (pin != pinconfirm)//check if passwords match
             {
                 cout<< "\n\nPasswords do not match, please try again.\n\n\n";
             }
             else
             {
                 loop = 0; //break loop of setting pin
             }
        } while ( loop != 0 );
    }
    twomers, thank you also, I get the basic idea how to acomplish this now. I tried to run the code you had, but the compiler gave an error with 'VK_RETURN' as it has not been declared as a variable. Am I supposed to replace this with something?

    Help apreciated, thanks.

    EDIT:

    I have declared 'VK_RETURN' as an int variable, and it works fine, apart from when I press return, it just comes up with an additional star, any ideas? Thanks.
    Last edited by stuartg123; 04-15-2007 at 01:31 PM.

  5. #5
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Could just replace it by 0x0D I guess. It's a virtual key code for return. I thought it was in windows.h. Did you include that?

    You should replace your char[5]'s with strings.

    >> pin != pinconfirm
    I think you'll need a strcmp() for that. You can't use equality, or inequality operators for character arrays, but you can with std::strings.

  6. #6
    Registered User
    Join Date
    Apr 2007
    Posts
    7
    Quote Originally Posted by twomers View Post
    I thought it was in windows.h. Did you include that?
    Ok, so I included windows.h, it all works fine now.

    Im still not totally sure about what to do to fix the 4digit problem. Im not totally clued up on strcmp() yet.

    As I say, im pretty new to all this, it probably all sounds very simple to you people, but thanks again.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    to compare the 2 char[]s for equality, use:
    Code:
    if(strcmp(firstString,secondString) == 0)
    however, since this is C++, you should use 'string' variables instead of 'char[5]'s. then when you compare two 'strings' you can just use:
    Code:
    if (firstString == secondString)
    if your going to use C++ style 'string's then include <string>.
    example:
    Code:
    #include<iostream>
    #include<string>
    
    using namespace std;
    
    int main()
    {
     string firstString = "hi";
     string secondString = "hi";
     
     if (firstString == secondString)
        cout << "strings equal";
     else
         cout << "not equal";
     
     cin.get();
     
    }
    Last edited by nadroj; 04-15-2007 at 02:17 PM.

  8. #8
    Registered User
    Join Date
    Apr 2007
    Posts
    7
    Ok, thanks alot nadroj, thats working now, but now I have the problem that the user can still enter a number 3 digits or less and it will still accept it.

    Also, how would I make it so that the program does not just close if I enter more than 4 digits?

  9. #9
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    you could check the length of the string:
    Code:
    if (myString.length() != 4)
       ...

  10. #10
    Registered User
    Join Date
    Apr 2007
    Posts
    7
    The digit count was working fine after messing around with some code, I then added in the hide function which worked fine, but this stopped it from being able to detect when passwords are MORE that 4 digits, LESS than 4 digits still works, apart from zero digits. Any ideas? Here is the code i've got at the moment.

    Code:
    #include <iostream>
    #include <cstring>
    #include <string>
    #include <conio.h>
    #include <windows.h>
    using namespace std;
    
    int main ()
    {
        int loop, loop2;
        do//set pin loops
        {
             string pin;
             string pinconfirm;
             loop = 1;
             loop2 = 1;
             do 
             {
                 cout<< "Please set 4 digit pin code: ";//set pin
                 while ( (pin += getch()).at(pin.size()) != VK_RETURN )
                 std::cout<< "*";
                 if(pin.size() == 4)
                 {
                     cout<< "\n\nThis is not 4 digits, please try again.\n\n\n";
                 }
                 else
                 {
                      loop2 = 0;
                 }
             } while ( loop2 != 0 );
             cout<< "\n\nPlease confirm: ";//confirm pin
             while ( (pinconfirm += getch()).at(pinconfirm.size()) != VK_RETURN )
             std::cout<< "*";//enter confirmation
             if(pin != pinconfirm)//check if passwords match
             {
                 cout<< "\n\nPasswords do not match, please try again.\n\n\n";
             }
             else
             {
                 loop = 0; //break loop of setting pin
             }
        } while ( loop != 0 );
    }
    Thanks, Stuart

  11. #11
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Code:
                 if(pin.size() == 4)
                 {
                     cout<< "\n\nThis is not 4 digits, please try again.\n\n\n";
                 }
    quit playing games with the user!

  12. #12
    Registered User
    Join Date
    Apr 2007
    Posts
    7
    Quote Originally Posted by nadroj View Post
    Code:
                 if(pin.size() == 4)
                 {
                     cout<< "\n\nThis is not 4 digits, please try again.\n\n\n";
                 }
    quit playing games with the user!
    Well they should learn to read!

  13. #13
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    There's a ton of unneeded things in that code.

    Code:
    #include <conio.h>
    #include <iostream>
    #include <string>
    
    int main ( void )
    {
        std::string pin, pinconfirm = "s";
        // initialise them to different values. 
    
        while ( pin != pinconfirm )
        {
            pin.clear();
            pinconfirm.clear();
    
            std::cout<< "Enter pin: ";
            while ( pin.size() != 4 && (pin += getch()).at(pin.size()) != VK_RETURN )
                std::cout<< "*";
    
            std::cout<< "\n\nConfirm pin: ";
            while ( pinconfirm.size() != 4 && (pinconfirm += getch()).at(pinconfirm.size()) != VK_RETURN )
               std::cout<< "*";
        }
    
        std::cout<< pin << ", " << pinconfirm;
        // Probably not a good idea in reality :)
    
        return 0;
    }
    works just fine.

  14. #14
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by stuartg123 View Post
    Here is the code i've got at the moment.
    And what if I type a digit incorrectly and hit backspace to correct it? It won't do the right thing.

  15. #15
    Registered User
    Join Date
    Apr 2007
    Posts
    7
    Wow, thats made it alot better and works fine . Just a few tweaks like linebreaks and then I've got the first part of my script done.

    Thanks, Stuart

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating counters and code not working
    By geekrockergal in forum C Programming
    Replies: 2
    Last Post: 02-05-2009, 12:50 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. Instant messenger app help
    By AusTex in forum C Programming
    Replies: 2
    Last Post: 05-01-2005, 12:41 AM
  4. comparing user input
    By lambs4 in forum C Programming
    Replies: 5
    Last Post: 12-15-2002, 10:28 AM
  5. Stopping a user from typeing.
    By knave in forum C++ Programming
    Replies: 4
    Last Post: 09-10-2001, 12:21 PM