print * instead of character

This is a discussion on print * instead of character within the C++ Programming forums, part of the General Programming Boards category; hi, I'm new in C++. I want to know is it possible to get a string and showing * instead ...

  1. #1
    Registered User
    Join Date
    May 2008
    Location
    IR, Iran
    Posts
    103

    print * instead of character

    hi,

    I'm new in C++. I want to know is it possible to get a string and showing * instead of characters. e.g. for getting password from user.

    I asked this from one of my friends and he said that I should set something in flags, but he didn't remember exactly what should I do.

    as I said I'm new in c++, I decided to ask it in here

    thanks, Behzad

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    We get this question about 5 times a day. Use the search function.

  3. #3
    Registered User
    Join Date
    May 2008
    Location
    IR, Iran
    Posts
    103

    Unhappy

    But I couldn't find what i need. if I did I would not post this.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I think that the short answer is that it is quite hard (if not impossible) with the standard libraries. You'll actually find that this is much easier in GUI (e.g you just set a flag when creating a text-box and Windows takes care of masking the password).

    However, locale's are a completely dark territory to me, but wouldn't it be possible to create a locale or facet that masks input? If I'm not mistaken locale's can transform the (input?) stream's contents or would that be hopeless?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183

  6. #6
    Registered User
    Join Date
    May 2008
    Location
    IR, Iran
    Posts
    103
    I read that topic, but I didn't get anything .
    I know I'm dumb

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,020
    No real standard way to do it but include <conio.h> and use getch() in a while loop. Getch reads the keyboard input in a character-by-character method.
    What's so difficult about understanding this?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    May 2008
    Location
    IR, Iran
    Posts
    103
    sorry
    i get it

  9. #9
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I would use a library that is platform independant such as ncurses or something. That way you can at least easily port your code if you are so inclined.

  10. #10
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,702
    Here's the *best* implementation I've ever seen for Windows: Masking password input

    gg

  11. #11
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Someone is a little self-promoting today. It looks good to me though. So you are allowed to be self-promoting this time I have an entire conio library that I wrote a while back since its not exactly the most portable header in the world.

  12. #12
    Registered User
    Join Date
    May 2008
    Location
    IR, Iran
    Posts
    103

    Talking

    I'm using this code. it seems perfect
    Code:
    #include <iostream>
    #include <stdexcept>
    #include <string>
    #include <windows.h>
    using namespace std;
    
    string getpassword( const string& prompt = "Enter password> " )
      {
      string result;
    
      // Set the console mode to no-echo, not-line-buffered input
      DWORD mode, count;
      HANDLE ih = GetStdHandle( STD_INPUT_HANDLE  );
      HANDLE oh = GetStdHandle( STD_OUTPUT_HANDLE );
      if (!GetConsoleMode( ih, &mode ))
        throw runtime_error(
          "getpassword: You must be connected to a console to use this program.\n"
          );
      SetConsoleMode( ih, mode & ~(ENABLE_ECHO_INPUT | ENABLE_LINE_INPUT) );
    
      // Get the password string
      WriteConsoleA( oh, prompt.c_str(), prompt.length(), &count, NULL );
      char c;
      while (ReadConsoleA( ih, &c, 1, &count, NULL) && (c != '\r') && (c != '\n'))
        {
        if (c == '\b')
          {
          if (result.length())
            {
            WriteConsoleA( oh, "\b \b", 3, &count, NULL );
            result.erase( result.end() -1 );
            }
          }
        else
          {
          WriteConsoleA( oh, "*", 1, &count, NULL );
          result.push_back( c );
          }
        }
    
      // Restore the console mode
      SetConsoleMode( ih, mode );
    
      return result;
      }
    
    int main()
      {
      try {
    
        string password = getpassword( "Enter a test password> " );
        cout << "\nYour password is " << password << endl;
    
        }
      catch (exception& e)
        {
        cerr << e.what();
        return 1;
        }
    
      return 0;
      }
    not using conio.h anymore.

  13. #13
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    It looks good to me I am not opposed to using the Win32 API. If you patrol the forums enough you will see this question posted frequently. Now you can simply supply anyone who asks with a link to your code

    [edit]Wait... why are you using ReadConsoleA() and WriteConsoleA() instead of just ReadConsole() and WriteConsole()? Just don't compile it for unicode and you should be fine.[/edit]

  14. #14
    Registered User
    Join Date
    May 2008
    Location
    IR, Iran
    Posts
    103
    Quote Originally Posted by master5001 View Post
    Wait... why are you using ReadConsoleA() and WriteConsoleA() instead of just ReadConsole() and WriteConsole()? Just don't compile it for unicode and you should be fine.
    I find this code in Cplusplus.com and just use it
    because this code is beyond my knowledge
    I'm very new in C++ ( about 2 weeks )
    Last edited by behzad_shabani; 10-03-2008 at 05:10 PM.

  15. #15
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Well I have no issue with writing websites emails about the nature of their coding. So that is on my todo list

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 05:23 AM
  2. Character problem!!
    By cBegginer in forum C Programming
    Replies: 3
    Last Post: 09-03-2005, 12:51 AM
  3. Replies: 20
    Last Post: 08-21-2005, 08:49 PM
  4. Any help would be appreciated.....
    By SprinterSteve in forum C Programming
    Replies: 6
    Last Post: 05-01-2003, 10:25 AM
  5. What kind of programs should I start writing?
    By Macabre in forum C++ Programming
    Replies: 23
    Last Post: 04-12-2003, 09:13 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21