Just wondering how to echo user input (e.g. password) as asterisks?
Any suggestions plz?
Thom
Printable View
Just wondering how to echo user input (e.g. password) as asterisks?
Any suggestions plz?
Thom
ive just answered this on the windows board. I hope you meant for windows....
Dont cross post... pick the forum that best will solve the problem and stick with it.
Yeah sorry about that, I didn't actually mean to post it in the windows section.
I meant in DOS, can it be done?
I'm using Vis C++ 6, Win32 Console Application.
Cheers.
You can use getch() (in conio.h) to read characters without echo-ing them on the screen and then use cout<< or printf() to output an asterisk for every character read.
As an example:
Code:#include <iostream>
#include <conio.h>
const char ENTER = 13;
int main()
{
char* str;
char key;
int i = 0;
while(1)
{
key = getch();
if(key == ENTER)
break;
str[i] = key;
std::cout << '*';
i++;
}
return 0;
}
> char* str;
Uninitialised pointer alert!!!!!
Hehe I thought that, and I'm a newbeh!
I'll give it a try though, thanks. Isn't there a simpler way?
Write some code to start if off. It might be worthwhile to scan their input twice using different methods and compare the results to each other. Than if they match, compare it to a password key.
Erm... sorry I don't follow.
Also, what is that .NET thing all about?
A little bit tricky. Works on Microsoft but not on *NIXCode:#include<stdio.h>
#include<conio.h>
int main()
{
char buffer[12];
int i, ch;
printf("Enter a password (10 alphanumeric sequenc max):\n" );
printf("Password: ");
/* Read password but no larger than 10 characters */
for( i = 0; i < 10 ; i++ )
{
//get character
ch = _getch();
//if user enters newline than quit
if(ch == '\r') break;
//if user enters backspace
if(ch == '\b')
{
//if there are no preceeding letters
if(i == 0)
{
--i;
continue;
}else //if there are preceeding letters
{
printf("\b%c\b",' ');
i-=2;
}
}else //no backspace was entered
{
putc('*',stdout);
buffer[i] = (char)ch;
}
}
/* Terminate string with null character: */
buffer[i] = '\0';
printf( "\n%s", buffer);
return 0;
}
If you wanted is cross platform than this should do it, chang this line:
To this:Code:if(ch == '\r') break;
Code:if(ch == '\r' || ch == '\n') break;