-
May 8th, 2002, 01:39 AM
#1
Thread Starter
Addicted Member
Password Mask with cin
Is there anyway to make it so when a user types at the cin prompt it shows "*" instead of what they are typing. You know for a password
Thanks in advance
C, C++ and none of that MCF crap either!
-
May 8th, 2002, 02:11 PM
#2
Frenzied Member
never used this but possibly like this:
Code:
int key;
while(key!=enter) // I don't know the value
{
key = getch();
cout<<"*";
//add key to your string
}
getch() is in conio.h
-
May 8th, 2002, 02:16 PM
#3
Fanatic Member
wouldnt that show up as
m*y*p*a*s*s*w*o*r*d?
Visit www.fragblast.com
Gaming, forums, and a online RPG/Battle system
(__Flagg) DOT NET? is this a Hindi Dating service?
-
May 8th, 2002, 02:19 PM
#4
Frenzied Member
eh....not sure......let me try it out
-
May 8th, 2002, 02:22 PM
#5
Frenzied Member
no, because there is no cin statement, nothing is being outputted but the asterisk. getch() doesn't print out what you entered.
-
May 8th, 2002, 04:05 PM
#6
Thread Starter
Addicted Member
Cool thanks Steve,
The only problem is that it doesnt print to the screen when you are in the loop, it waits until afterwerds. Also it wont print out the cout i have above the loop.
Uhm, damn i am a n00b, also I am not sure how to put the key value into a char array. I tried useing strcat but it complains about it not being the right type.
Sorry btw I program in VB and have been trying to move on to C but it is no easy task in my opinion. Thanks for your help.
C, C++ and none of that MCF crap either!
-
May 8th, 2002, 05:04 PM
#7
Thread Starter
Addicted Member
also how can i remove a character if they hit backspace. Well if the character is backspace, the ascii i can figure out, how do i remove the last character from a char array?
Thanks
C, C++ and none of that MCF crap either!
-
May 8th, 2002, 05:09 PM
#8
Frenzied Member
To remove the last character from a string, you can just replace it with a '\0' null terminator character. As in ASCII zero.
Harry.
"From one thing, know ten thousand things."
-
May 8th, 2002, 05:16 PM
#9
Thread Starter
Addicted Member
uhm exactly how would i go about that, and what would it be doing? Sorry i am VERY new to c++.
Thanks
C, C++ and none of that MCF crap either!
-
May 8th, 2002, 07:26 PM
#10
Member
Try this code. I think it does what you want. The only problem is that the backspace routine works really slow. Anyone have a better idea than using system("cls")?
PHP Code:
char password[256] = ""; // 256 chars for the password
char tempchar[2] = ""; // Temporary character array
char key; // Holds the key number
while(key != 13 /*Enter*/)
{
key = getch(); // Grab a character
if (key == 8 /*Backspace*/) {
password[strlen(password)-1] = 0; // Erase the last character
system("cls"); // Clear the screen (a bit slow though)
for (int i=0; i<strlen(password); i++)
cout << "*"; // Throw "*"'s on the screen
flush(cout); // Flush output to the screen
continue; // Start loop over again
}
cout << "*"; // Print a "*"
flush(cout); // Flush output to the screen
tempchar[0] = key; // Copy the char to an array
tempchar[1] = 0; // with a null at the end
strcat(password, tempchar); // Append the character to the password array
}
cout << endl << password << endl; // Print the inputed password
-
May 13th, 2002, 08:46 PM
#11
Thread Starter
Addicted Member
odd output
User Name:josh
Password:*¨d¨d¨d¨d¨d¨d¨d¨djoshn
when i run my program i use cin.getline to get the value for the username then i use the idea posted above for getting the password. It wasnt working right for me so i added a cout << password in the loop to see what the password looked like. This is the output i got when i typed in a username of josh and typed just n at the password promt.
here is my code if it makes it easyer
Code:
#include <iostream>
#include <windows.h>
#include <string>
#include <conio.h>
using namespace std;
int main(int argc, char *argv[])
{
//If they passed at least one parameter run the main code
if (argc > 1)
{
//Initialize the variables that we will need
char username[8];
char password[8];
char together[17];
char tempchar[2];
int count = 1;
int key;
//Ask for the username and place it in the variable username
cout << "User Name:";
cin.getline(username,255,'\n');
cout << "Password:";
//Loop untill ENTER is pressed.
while(key!=13)
{
//Set key to the key that was pressed
key = getch();
if (key == 8)//Backspace was pressed
{
password[strlen(password)-1] = 0; // Erase the last character
}
else
{
if (count <= 8)//If it is less than 8 add to the array still
{
//Output an * instead of what was typed
cout << "*";
//Increment count by one each time throught the loop so if it gets above 8
//dont add any more Chars to password or anymore "*" to the console.
count = (count + 1);
//This will place each key into the password variable
tempchar[0] = key; // Copy the char to an array
tempchar[1] = 0; // with a null at the end
strcat(password, tempchar); // Append the character to the password array
///////////////////////////////////////////////////////////////////
//DEBUG////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
cout << password;
}
}
}
cout << "\n";
//Copy username + space + password to the variable together
strcpy(together, username);
strcat(together, " ");
strcat(together, password);
//run the batch file with the first argument as the name of the file.
ShellExecute(NULL, "open", "test", together, NULL, SW_SHOWNORMAL);
return 0;
}
else
{
cout << "input.exe takes one paramiter, the name \nof a file that it will pass two parameters to.\n";
cout << "it will ask for a User Name and Password \nand then pass them to the file named.\n";
cout << "\n";
cout << "Josh Powers\n";
return 0;
}
}
C, C++ and none of that MCF crap either!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|