Password

How can I change the character when typing a password from letter to asterisk(*)? Thanks!
I need a bit more information.

How do you get the data input and where do you want the data output?
I want to make a sign in form then log in later. This is the codes:

1
2
3
4
5
6
7
8
9
10

string password;

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

cout << endl << endl;

cout << "Password: "; 



When Im typing in "Enter your password, I want to change it to asterisk.
it won't work that way.
simple as that.

the terminal prints the caracters as soon as they are entered
So how can I make the displaying password is asterisk rather than letters and numbers?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
#include <conio.h>
using namespace std;

int main()
{
    std::string password;
    std::cout << "Enter your password: ";
    char temp = getch();

    while(temp != '\r')
    {
        std::cout << '*';
        password += temp;
        temp = getch();
    }

    std::cout << "\nYour password is " << password << std::endl;
    //getch();
    return 0;
}


Note: conio.h is depricated
We can't give you a simple answer because there is no way to do what you ask for using only standard C++. Other library/system functions are needed to do it. You can probably find a solution in the links above, but note that many of them does not work on all platforms.
Last edited on
This is my codes so far. The problem is that, when you put wrong password on the first try, then you put the correct password on the second time, it will not recognize the right 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
#include <iostream>
#include <string>
#include <conio.h>
using namespace std;

int main()

{
   string pass ="", newPass;
   char ch, ch1;
   bool isRunning = true;

   cout << "Enter pass: ";

   ch = _getch();

   while(ch != 13)
   
   {//character 13 is enter
      pass.push_back(ch);
      cout << '*';
      ch = _getch();
   }
	

   cout << endl << endl;

   
  do
  {

   cout << "\n\nEnter your pass: ";
      
	ch1 = _getch();

   while(ch1 != 13)
   
   {//character 13 is enter
      newPass.push_back(ch1);
      cout << '*';
	  ch1 = _getch();
   }

	if (pass == newPass)
	{
		isRunning = false;
		cout << "\n\nAWW!";
	}
  }while(isRunning);

_getch();
return 0;
}
Last edited on
You need to newPass.clear(); before you iterate the next try
Yaaaaaay! Thanks Bro coder777. Its working now! :)
Topic archived. No new replies allowed.