Password input

for when im imputing a password into the command prompt is there a way i can make it "*" instead of the actual text to hide what is being typed.
1
2
3
4
5
6
7
#include <conio.h> //dont judge me
char ch;
do
{
    ch = getch();
    putchar('*');
}while(ch != '\n');
not exactly what im looking for here is my code

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
#include <iostream>
#include <string>

using namespace std;

int main()
{
	string create_password;
	string confirm_password;
	string word_password;
	string enter_password;

	while (true)
	{
		cout << "Create password" << endl;
		getline(cin, create_password);
		cout << "Confirm password" << endl;
		getline(cin, confirm_password);

		if (create_password == confirm_password)
		{
			cout << "Passwords match" << endl << "Password set" << endl;
			word_password = create_password;
			break;
		}
		else
		{
			cout << "Passwords do not match please try again" << endl;
			continue;
		}
	}

	while (true)
	{
		cout << "Enter password: ";
		getline(cin, enter_password);

		if (enter_password == word_password)
		{
			cout << "Password is correct" << endl;
			break;
		}
		else
		{
			cout << "Password is Incorrect" << endl;
			continue;
		}
	}
}
I think it is.
getline(cin,create_password)
You are getting input one character at a time until ENTER is pressed.

Archieve it this way
1
2
3
4
5
6
7
8
char ch;
string create_password;
do
{
    ch = getch();
    create_password.push_back(ch);
    putchar('*');
}while(ch != '\n');
What code should i be replacing with this.
1
2
3
4
5
6
7
8
9
10
11
void inpuPassword(string &password)
{
    char ch;
    do
    {
        ch = getch();
        password.push_back(ch);
       putchar('*');
    }while(ch != '\n');
    return ;
}


call it like this.
1
2
cout << "Create password" << endl;
inputPassword(create_password);


So it replaces all getline in your code


Last edited on
Topic archived. No new replies allowed.