Password

Hi :)
I have finally figured out how to make *password input. But, it's kinda working weird :/
If I do it like this:
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
#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(const char *prompt, bool show_asterisk=true)
{
	const char BACKSPACE=127;
	const char RETURN=10;
	
	string password;
	unsigned char ch=0;
	
	cout <<prompt<<endl;
	
	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;
}


int main()
{
     string pass=getpass("Password: ",true);
	}
return 0;
}
It's working nice. BUT as I put it into a cycle, so I can check for right password, it's NOT waiting for me to enter the 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#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(const char *prompt, bool show_asterisk=true)
{
	const char BACKSPACE=127;
	const char RETURN=10;
	
	string password;
	unsigned char ch=0;
	
	cout <<prompt<<endl;
	
	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;
}


int main()
{	
	string login;
	int i=0;
	while (i==0){
		cout << "Login: ";
		cin >> login;
		string pass=getpass("Password: ",true);
		//check user...
		cout << "Logged in as "<<login<<"\n";
	}
		return 0;
}

and the output is like:
1
2
3
4
5
Login: john
Password: 

Logged in as john
Login: 
No, the problem isn't cycle, it's the key "RETURN" that I enter after "login". So? How to avoid that?
Don't mix getch and company with cin. They don't work well together.
I've just figured it :)
cin.ignore(2048, '\n'); after Login and it works flawless^^
Topic archived. No new replies allowed.