log in program (password question)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>

using namespace std;

int main( )
{
    string userEnterPassword;
    string password( "hello" );

    cout << "Enter your password: ";
    cin >> userEnterPassword;

    if ( userEnterPassword == password )
    {
        cout << "Right password...\n";

    } else {

        cout << "Wrong password...\n";
    }

    return( 0 );
}


here is a really basic idea of the program I want to write. I was jw how do you make it so that the password show up in all ******.. Any help would be nice, articles, tutorials, or examples....
closed account (S6k9GNh0)
You make a loop event that polls for key events. When a key event is pressed, instead of printing the key, print an asterik or nothing. This is a guess, I'm not really sure how. Good luck!
try this one:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <string>
#include <conio.h>

using namespace std;

int main( )
{
    string userEnterPassword;
    string password( "hello" );

    cout << "Enter your password: ";
    
    char ch;
    char buffer[80];
    int count=0;
    while (true)
    {
        ch=getch();
        if (ch==13) {buffer[count]='\0'; break;}  //13->enter
        if (ch!=8) //8-> backspace
        {
           buffer[count]=ch;
           cout << '*';
           count++;
        }
        else if (count>0)
        {
            cout << '\b';
            cout << ' ';
            cout << '\b';
            count--;
        }
    }
    cout << endl;
    
    userEnterPassword=buffer;
       
    if ( userEnterPassword == password )
    {
        cout << "Right password...\n";

    } else {

        cout << "Wrong password...\n";
    }

    cout << "hit enter to quit..." << endl;
    cin.get();
    return 0;
}
It is really amazing how often this kind of question comes up.
But more amazing is how often people whip out the old <conio.h> stuff to answer it.

Avoid conio.h. It is not a safe bet on modern systems. Fortunately, there is help:
http://sourceforge.net/projects/conio/


Also, don't use asterisks. That is a modern GUI mistake from the guys at Redmond. Asterisks only tell people how long your password is.

Here is how to do it cross-platform (Windows and POSIX):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <string>

#if defined(__WIN32__)
  #include <windows.h>

  std::string getpassword()
    {
    std::string result;
    HANDLE      hstdin;
    DWORD       mode;
    BOOL        isconsole;

    hstdin    = GetStdHandle( STD_INPUT_HANDLE );
    isconsole = GetConsoleMode( hstdin, &mode );

    if (isconsole)
      SetConsoleMode( hstdin, mode & ~(ENABLE_ECHO_INPUT) );

    std::getline( std::cin, result );

    if (isconsole)
      SetConsoleMode( hstdin, mode );

    return result;
    }

#else
  #include <unistd.h>
  #include <termios.h>

  std::string getpassword()
    {
    std::string    result;
    struct termios echo;
    struct termios noecho;
    bool           isconsole;

    isconsole = isatty( 0 );

    if (isconsole)
      {
      tcgetattr( 0, &echo );
      noecho = echo;
      noecho.c_lflag &= ~(ECHO);
      tcsetattr( 0, TCSANOW, &noecho );
      }

    std::getline( std::cin, result );

    if (isconsole)
      tcsetattr( 0, TCSANOW, &echo );

    return result;
    }

#endif


int main()
  {
  using namespace std;
  string pwd;

  cout << "Please enter a (fake) password> " << flush;

  pwd = getpassword();

  cout << "\nGood job. Your password is \"" << pwd << "\"\n";

  return 0;
  }


If you really want those stupid stars:

Windows: http://www.cplusplus.com/forum/general/3570/#msg15410
Linux: http://www.cplusplus.com/forum/beginner/1988/page4.html#msg14522

The Linux example has code to set raw input. You will use it to replace lines 12-19 and 42 in the Windows example.

Good luck!
Topic archived. No new replies allowed.