hey, been working on a program and I had a need for a password line (btw I am using linux w/ gcc compiler). while I was making it I checked the net and found there's no getch() for linux, so I frankensteined one from several sources and got it to work. Only problem is, it doesn't register backspace ('\b'), so people can't correct mistakes. I tried using a different key instead of backspace, like \t for example, it worked as it should, getchar() registered it, it just doesn't seem to like \b. here's the code:
#include <cstring>
#include <iostream>
#include <unistd.h>
using namespace std;
#ifndef KBHITh
#define KBHITh
#include <termios.h>
class keyboard
{
public:
keyboard();
~keyboard();
private:
struct termios initial_settings, new_settings;
};
#endif
keyboard::keyboard()
{
tcgetattr(0,&initial_settings);
new_settings = initial_settings;
new_settings.c_lflag &= ~ICANON;
new_settings.c_lflag &= ~ECHO;
new_settings.c_lflag &= ~ISIG;
new_settings.c_cc[VMIN] = 1;
new_settings.c_cc[VTIME] = 0;
tcsetattr(0, TCSANOW, &new_settings);
}
keyboard::~keyboard()
{
tcsetattr(0, TCSANOW, &initial_settings);
}
int main()
{
keyboard my_board; //essentially removes line buffer
string pass="";
char c=' ';
cout << "Testing passwords, enter a string to passwordize\n";
while(c != '\n')
{
c = getchar();
if(c != '\n')
{
if(c == '\b') //doesn't like this line
{
pass=pass.substr(0,pass.size()-1);
cout << "\010 \010" << flush;
}
else
{
pass.push_back(c);
cout << "*" << flush;
}
}
}
my_board.~keyboard(); //reverts to normal line buffering
cout << "\nYou entered: \"" << pass << "\"" << endl;
return 0;
}
I've heard rumors of getchar not being able to read backspaces, but I wasn't sure. Also, I have no doubt there are better ways to emulate getch(), if you know of one please point me in the right direction. Any help would be appreciated.
~J