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
Printable View
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
never used this but possibly like this:
getch() is in conio.hCode:int key;
while(key!=enter) // I don't know the value
{
key = getch();
cout<<"*";
//add key to your string
}
:)
wouldnt that show up as
m*y*p*a*s*s*w*o*r*d?
eh....not sure......let me try it out
no, because there is no cin statement, nothing is being outputted but the asterisk. getch() doesn't print out what you entered. :)
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.
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
To remove the last character from a string, you can just replace it with a '\0' null terminator character. As in ASCII zero.
uhm exactly how would i go about that, and what would it be doing? Sorry i am VERY new to c++.
Thanks
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
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;
}
}