Linux show asterisks on password input

I used example 1 here: http://www.cplusplus.com/articles/E6vU7k9E/

I'm only partially understanding what's going on. I'm not getting any errors so I don't know how to fix this issue. It's not letting me type a password unless I get the username wrong.
I had to modify it slightly to fit my current project. this is what I have:

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
#include <termios.h>
#include <unistd.h>
#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;

int getch() {
    int ch;
    struct termios t_old, t_new;

    tcgetattr(STDIN_FILENO, &t_old);
    t_new = t_old;
    t_new.c_lflag &= ~(ICANON | ECHO);
    tcsetattr(STDIN_FILENO, TCSANOW, &t_new);

    ch = getchar();

    tcsetattr(STDIN_FILENO, TCSANOW, &t_old);
    return ch;
}





string getPASS(bool show_asterisk=true)
{
   const char BACKSPACE=127;
   const char RETURN=10;

   string password;
   unsigned char ch=0;

   cout << "Password: ";
   while((ch=getch())!=RETURN)
     {
        if(ch==BACKSPACE)
          {
             if(password.length()!=0)
               {
                  if(show_asterisk)
                  cout <<"\b \b";
                  password.resize(password.length()-1);
               }
          }
        else
          {
              password+=ch;
              if(show_asterisk)
                  cout <<'*';
          }
     }
   cout <<endl;
   return password;
}


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
int main()
{
   cout << endl;
   while (attempts == 3)
   {
      cout << "Too many attempts have been made! Exiting..." << endl; return 0;
   }
   string USER;
   cout << "Username: "; cin >> USER; string password = getPASS(true);
   if (USER == "josh") 
      {
         if (password == "hsoj")
         {
            cout << "\nAccess granted!\n" << endl;
            MENU();
         }
         else
         {
            cout << "\nAccess denied!" << endl; 
            attempts = attempts + 1;
            main();
         }
      }
   if (USER == "brian") 
      {
         if (password == "nairb")
         {
            cout << "\nAccess granted!\n" << endl;
            MENU();
         }
         else
         {
            cout << "\nAccess denied!" << endl; 
            attempts = attempts + 1;
            main();
         }
      }
   else
   {
      getPASS();
      cout << "\nAccess denied!" << endl; 
      attempts = attempts + 1;
      main();
   }
}


This is the output:

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
[josh@Craptop ~]$ g++ -Wall -Wextra ./test.cpp -o test
[josh@Craptop ~]$ ./test

Username: josh
Password: 

Access denied!

Username: josh
Password: 

Access denied!

Username: asd
Password: 
Password: ****

Access denied!

Too many attempts have been made! Exiting...
Password: ***

Access denied!

Username: josh
Password: 

Access denied!

Username: josh
Password: 

Access denied!

Username: ^C
[josh@Craptop ~]$ 

EDIT: (this stuff down here and multiple grammer fixes)

The blank password fields were just displayed and passed by like it's hitting enter for me or something, and the Too many attempts have been made! Exiting... didn't even exit.
Please, if anyone can help me with this! And explain in detail, so I know why this is happening, please?
Last edited on
Calling main() recursively? Not cool, you need to fix this.

You're reading the user id and password using two different methods. The first time you call getPASS(), it sees the unprocessed <CR> left over from reading the user id followed by <CR>. So it returns immediately with an empty string.
Last edited on
Topic archived. No new replies allowed.