Making password like "*" in a console program

How to make a "password" field in a CONSOLE program in C++(t

Page 1 of 1

2 Replies - 10345 Views - Last Post: 16 September 2008 - 11:58 AM Rate Topic: -----

#1 dor1997  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 2
  • Joined: 04-September 08

Making password like "*" in a console program

Post icon  Posted 16 September 2008 - 07:18 AM

How to make a "password" field in a CONSOLE program in C++(the field should show "*"s instead of the pasword entered)?
for example:
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
string user,pass;
cout <<"Login\nPlease enter your username: \a";
cin >> user;
cout <<"Please enter your password: \a";
cin >> pass; //here I want to make it show "*" insted of input
if(user == "/*some username*/" && pass == "/*some password*/")
{
cout <<"Success! You're logged in!\n";
//...
}
}


note: It's the code of my program that I need the password field for, it's just an example, to explain the problem with a very short and not confusing code...
So plz help me, coz I've no idea how to fix it, and it's pretty important for my program...

Is This A Good Question/Topic? 1

Replies To: Making password like "*" in a console program

#2 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1376
  • View blog
  • Posts: 10,951
  • Joined: 06-February 08

Re: Making password like "*" in a console program

Posted 16 September 2008 - 11:41 AM

Try my snippet:
http://www.dreaminco...snippet2084.htm

Hope this helps :)
Was This Post Helpful? 0
  • +
  • -

#4 Skaldi  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 5
  • Joined: 16-September 08

Re: Making password like "*" in a console program

Posted 16 September 2008 - 11:58 AM

Hello dor1997

If u don't want the user to see what he or she is typing you shouldn't use the Console Stream.

A good solution could be that:


#include <iostream>
#include <string.h>
#include <conio.h>
#include <stdio.h>


void InputPassword(string &value);

int main(void)
{
   string user,pass;
   cout <<"Login\nPlease enter your username: \a";
   cin >> user;
   cout <<"Please enter your password: \a";

   InputPassword(pass); //The pass object is passed as Reference. So you can use it after this function call
}

void InputPassword(string &value)
{
	char key;

	do
	{
		 key = getch();

		 switch (key)
		 {
				 case '\b': //backspace pressed
					  if(value.length() > 0)
					  {
							value.erase(value.length() - 1, 1);

							//Erase the last Character in password signs
							std::cout << '\b' << " " << '\b';
					  }

				 break;

				 default:
					 if(key > 31 && key < 127)   //Only no control characters are valid
					 {
							value.push_back(key);
							std::cout << "*";
					  }

		  }

	} while(key != '\r') //Quit if Enter is Pressed
}




This code isn't tested, but should work.

Hope this helps

This post has been edited by born2c0de: 19 September 2008 - 06:25 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1