The title says it all; please tell me how to make text appear as an asterix.
The title says it all; please tell me how to make text appear as an asterix.
text inputed by the user
You can do that with getch() as well. Remember the last post I told you how to do it? Just do getch() in a loop, echo an asterisk, and concatenate the character onto a string.
Code:#include <iostream> #include <conio.h> using namespace std; int main() { char keyPress; string password = ""; cout << "Please enter your password: "; do { keyPress = getch(); if (keyPress != char(13)) { // Value of Enter password += keyPress; cout << "*"; } } while (keyPress != char(13)); cout << endl << password; return 0; }
Last edited by SlyMaelstrom; 11-10-2005 at 08:21 PM.
Sent from my iPadŽ
Heh, wow... it's right there, isn't it.
Sent from my iPadŽ
Thank you once again for your informative answers!
SlyMaelstrom, I would like to ask you about an earlier post-what if you want to input more that 1 character into getch(), but perhaps less than the limit in the program?
You're better of using the prog outlined in the FAQ since it handles backspace as well...
Why would you need to input more than one character into
getch() anyway?
Snippet taken from the FAQ...
Code:/* * An example of how to obtain unbuffered * input using the conio.h library and getch() */ #include <stdio.h> #include <conio.h> #include <ctype.h> #include <iostream> using namespace std; int main() { int ch; char pword[100]; int i = 0; cout<<"Enter your password\n>>"; //fflush(stdout); while ((ch = getch()) != EOF && ch != '\n' && ch != '\r' && i < sizeof(pword) - 1) { if (ch == '\b' && i > 0) { cout<<"\b \b"; //fflush(stdout); i--; pword[i] = '\0'; } else if (isalnum(ch)) { cout<<"*"; pword[i++] = (char)ch; } } pword[i] = '\0'; cout<<"\nYou entered >>"<< pword; cin.get(); cin.get(); return 0; }
I agree with treenef, my example was intentionally very trite and direct. The FAQ overs a more full program for your problem. It also gives some good tips.
getch() is a function that returns a character from your stdin. It doesn't hold anything and nothing from your end goes into it. As treenef said, as well, there is no need to. Just read in a character and concatenate to a string. It's simple.
Sent from my iPadŽ