English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Hi.

I'm trying to code a c++ console program which has a login system in the begining. I would want to hide the characters the user entered when they are typing in their password. How do i do that?

2007-04-16 19:17:52 · 6 answers · asked by cincauhangus 2 in Computers & Internet Programming & Design

an example:

my current login now displays this and the user enters their username and password:

Username : myUsername
Password : myPassword

But I want it to display like this:

Username: myUsername
Password: **********

How do i do that in C++ console?

2007-04-16 19:43:36 · update #1

an example:

my current login now displays this and the user enters their username and password:

Username : myUsername
Password : myPassword

But I want it to display like this:

Username: myUsername
Password: **********

How do i do that in C++ console?

2007-04-16 19:43:37 · update #2

6 answers

#include
#include
using namespace std;

int main() {

string strPass = "";
char c;
int StarNum = 0;

cout << "\n\tPassword : ";

while (c != 13)
{
c = (char)getch();
if(c == 13) break;
StarNum++;
strPass += c;
cout << "*";
}

//remove the following lines
cout << "\t\nYour password is : ";
cout << strPass << endl;
system("pause");
}

2007-04-16 20:32:36 · answer #1 · answered by iyiogrenci 6 · 0 0

do not get the input using cin statement.

Use getch() of .
getch() is special it reads from stdin but doesnot echos on screen(stdout)

So it can be placed in a for loop and you can store it in a charater array and display the * for every getch()

eg.

char a='a',str[50];
for(int i=0;a!=13;i++)
//13 is the ascii value for enter key
{
a=getch();
str[i]=a;
cout<<'*'; //to display the password sign.
}
str[i]='\0';
// to set the null character and to eliminate the 13(\n character at the last of the string.)

2007-04-17 01:18:39 · answer #2 · answered by Gopinath M 3 · 2 0

hi friend,
well i can help u , but this space is too less to explain
still let me try

int x; //declare an integer
cout<<"Please enter the pass length"; //take the password length from user
cin>>x; // user inputs the pass length
char pass[100]; //string declared to store pass
for (int i=0;i {
pass[i]=getch();
cout<<"*";
}
pass[i]='\0'; // last character of string is null character \0

//now u have obtained the password
//just check it out with a pre recorded password.
//for instance, if u have a password as "RAJ", stored in a file
//named "story.txt"
//just add the following code

ifstream fin;
fin.open("story.txt");
char array[100];
fin>>array;
if ( strcmp(array,pass)==0)
cout<<"Password matched, access granted"
else
cout<<"Wrong pass !!. please try again";



dont forget to add header files
#include // for ifstream
#include // for getch()
#include // for strcmp()



best of luck, well this code runs perfectly in turbo c++
by the way, if u want to record the password without taking the password length, then just replace some lines in above code.

if u still dont get it, just IM me, (i am generally invisible ,lol)
my id is :- rajvardhansolanki@yahoo.co.in
it was pleasure helping u out. best of luck again.

2007-04-16 19:34:30 · answer #3 · answered by chintu 2 · 0 0

Hi

I think u can use the getpass() function which will act similar to getstring() but will hide the characters that user enters

e.g

pass = getpass("Enter password :");

this will prompt "Enter Password : " and return the password entered into the variable "pass"


try..
Ha-p Computing..

2007-04-16 19:52:01 · answer #4 · answered by Weraw 2 · 0 0

That's a good question!

2016-08-23 23:53:08 · answer #5 · answered by Anonymous · 0 0

If you are using Turbo C++, you can use conio.h function getch() and purchar('*')


On Unix, uses curses.h for same effect

2007-04-16 23:15:40 · answer #6 · answered by kinshuk k 2 · 0 0

fedest.com, questions and answers