#include <iostream>
#include <limits> // <--- Added.
#include <string>
#include <conio.h>
int main(/*int argc, char * argv[]*/) // <--- If not used you do not need it here.
{
char u_pwd[50]{}; // <--- ALWAYS initialize your variables.
int count = 0;
std::cout << "Password: ";
while (true)
{
char ch = _getch();
if (ch == '\r') // <--- Or you could use (ch = 13).
{
u_pwd[count] = '\0';
break;
}
u_pwd[count] = ch;
std::cout << "*";
++count;
}
std::cout << "\n\nyour password is " << count << " long" << std::endl;
//std::cin.ignore(std::numeric_limits<int>::max(), '\n'); // <--- Not needed. There is nothing in the buffer to clear. Will wait fof something to be entered. An effictive pause.
std::cout << "\n\n\npress ENTER to close program."; // <--- Changed.
std::cin.get();
return 0;
}
Just a friendly warning: <conio.h> is not supported by all systems, such as Macintosh, which is what I use. So if you want to make sure your code is portable, use something else. I'm not saying don't use it, just if you're going to give/sell your code to clients, you want to make sure it is portable and doesn't have OS-dependent things like <conio.h>. You may already know this, in which case you can just ignore me.
you mean, actually, the original input from the keyboard is \r and then converted to \n by the OS?
Not quite. The original input is \r and is converted to \n by the CRT (C run time) - not the os. The os converts the keycode for the key pressed to the appropriate ASCII code. When you press a key, the os gets a signal that a key has been pressed and when it's been released together with its key code. This is how the OS knows that you have pressed the shift key or the ctrl key and and time how long the key(s) are pressed to generate repeat chars if held down etc. Low-level keyboard is actually quite complicated!
Also note that _getch() will not 'interpret' key strokes. So if you enter ctrl-c (which normally terminates a console program), _getch() will return 3!
@andy: yes, it's bcoz i used to use numeric_limits... not a prob, actually... and actually, that's not what i'm asking... i was more curious abt the escape chars...
@max: is there any function in standard C++ to replace _getch?
seeplus wrote:
Also note that _getch() will not 'interpret' key strokes. So if you enter ctrl-c (which normally terminates a console program), _getch() will return 3!
is there any references so we can find out all the original inputs?
so, after we pressed the keyboard, if the raw input not "captured" by, for example, _getch(), it will converted by CRT/OS/whatever?
They aren't a direct replacement. _getch() returns when a key is pressed and doesn't wait until a CR/LF has been entered. Also, it doesn't process the obtained char (eg you can't get a ctrl_c with the get functions). AFAIK, there's no standard C/C++ replacement.
.get() gets one char from the stream - after a line terminating char has been entered for keyboard input!
CR/LF carriage return/line feed. Enter is CR (ASCII 13) but CRT interprets this as a LF (ASCII 10) for line termination. So _getch() will give ASCII 13 for Enter but .get() will give ASCII 10.
Not allowed to. Can't use Boost either. Boo-hoo. Cry.....
I've looked at it for private use. I've also looked at the date library at https://github.com/HowardHinnant/date but again can't use for production code.