how to get backspace input from keyboard
by rahulvishwakarma from LinuxQuestions.org on (#58JE4)
hi to all, I've mysql server in centos6 server and codeblocks in centos 7client. i am trying to do autofill of
product name when we input some initial letters. this happned but it does not getting back when giving input of
backspace. here is mine code :-
Code:void product::productSearch()
{
Get *get = new Get;
clrscr();
string prdin = "";
string prd1 = "";
int l = get->printMenu(productRecords, "Product Search");
gotoxy(30, 5);
//cin >> prdin;
Getchar *getc;
char ch;
MYSQL_ROW row;
MYSQL_RES *res;
Conn::conn = Conn::connection();
do
{
ch = getc->getch();
if(ch == '\b')
{
if(prdin.length() > 0)
{
prdin = prdin.substr(prdin.length() -1);
}
}
else
{
prdin += ch;
}
sql = "select productname,stock, rate, productId from tableProductRecords where productname like '%"+prdin
+"%';";
int qstate = mysql_query(Conn::conn, sql.c_str());
if(!qstate)
{
res = mysql_store_result(Conn::conn);
if((row = mysql_fetch_row(res)) != nullptr)
{
gotoxy(30, 5);
cout << row[0];
gotoxy(30,6);
cout << row[1];
gotoxy(30, 7);
cout << row[2];
gotoxy(30, 8);
cout << row[3];
gotoxy(30 + prdin.length() , 5);
//ch = getc->getch();
}
}
else
{
gotoxy(10, 20);
cout << "error in product Enter : " << mysql_error(Conn::conn);
}
}while(ch != '\n');
clrscr();
drawrect();
}mine getch code is :-
Code:Read 1 character without echo
getch() function definition.
char Getchar::getch(void)
{
return getch_(0);
}
void Getchar::initTermios(int echo)
{
tcgetattr(0, &oldterm); //grab old terminal i/o settings
newterm = oldterm; //make new settings same as old settings
newterm.c_lflag &= ~ICANON; //disable buffered i/o
newterm.c_lflag &= echo ? ECHO : ~ECHO; //set echo mode
tcsetattr(0, TCSANOW, &newterm); //apply terminal io settings
}
/* Restore old terminal i/o settings */
void Getchar::resetTermios(void)
{
tcsetattr(0, TCSANOW, &oldterm);
}


product name when we input some initial letters. this happned but it does not getting back when giving input of
backspace. here is mine code :-
Code:void product::productSearch()
{
Get *get = new Get;
clrscr();
string prdin = "";
string prd1 = "";
int l = get->printMenu(productRecords, "Product Search");
gotoxy(30, 5);
//cin >> prdin;
Getchar *getc;
char ch;
MYSQL_ROW row;
MYSQL_RES *res;
Conn::conn = Conn::connection();
do
{
ch = getc->getch();
if(ch == '\b')
{
if(prdin.length() > 0)
{
prdin = prdin.substr(prdin.length() -1);
}
}
else
{
prdin += ch;
}
sql = "select productname,stock, rate, productId from tableProductRecords where productname like '%"+prdin
+"%';";
int qstate = mysql_query(Conn::conn, sql.c_str());
if(!qstate)
{
res = mysql_store_result(Conn::conn);
if((row = mysql_fetch_row(res)) != nullptr)
{
gotoxy(30, 5);
cout << row[0];
gotoxy(30,6);
cout << row[1];
gotoxy(30, 7);
cout << row[2];
gotoxy(30, 8);
cout << row[3];
gotoxy(30 + prdin.length() , 5);
//ch = getc->getch();
}
}
else
{
gotoxy(10, 20);
cout << "error in product Enter : " << mysql_error(Conn::conn);
}
}while(ch != '\n');
clrscr();
drawrect();
}mine getch code is :-
Code:Read 1 character without echo
getch() function definition.
char Getchar::getch(void)
{
return getch_(0);
}
void Getchar::initTermios(int echo)
{
tcgetattr(0, &oldterm); //grab old terminal i/o settings
newterm = oldterm; //make new settings same as old settings
newterm.c_lflag &= ~ICANON; //disable buffered i/o
newterm.c_lflag &= echo ? ECHO : ~ECHO; //set echo mode
tcsetattr(0, TCSANOW, &newterm); //apply terminal io settings
}
/* Restore old terminal i/o settings */
void Getchar::resetTermios(void)
{
tcsetattr(0, TCSANOW, &oldterm);
}