how do I read backspace (getchar)?

Please support our C++ advertiser: Download Intel® Parallel Studio Eval
Thread Solved

Join Date: Dec 2006
Posts: 111
Reputation: jesseb07 is on a distinguished road 
Solved Threads: 15
jesseb07's Avatar
jesseb07 jesseb07 is offline Offline
Junior Poster

how do I read backspace (getchar)?

 
0
  #1
Mar 8th, 2008
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:

  1. #include <cstring>
  2. #include <iostream>
  3. #include <unistd.h>
  4. using namespace std;
  5.  
  6. #ifndef KBHITh
  7. #define KBHITh
  8.  
  9. #include <termios.h>
  10.  
  11. class keyboard
  12. {
  13. public:
  14.  
  15. keyboard();
  16. ~keyboard();
  17.  
  18. private:
  19.  
  20. struct termios initial_settings, new_settings;
  21. };
  22.  
  23. #endif
  24.  
  25. keyboard::keyboard()
  26. {
  27. tcgetattr(0,&initial_settings);
  28. new_settings = initial_settings;
  29. new_settings.c_lflag &= ~ICANON;
  30. new_settings.c_lflag &= ~ECHO;
  31. new_settings.c_lflag &= ~ISIG;
  32. new_settings.c_cc[VMIN] = 1;
  33. new_settings.c_cc[VTIME] = 0;
  34. tcsetattr(0, TCSANOW, &new_settings);
  35. }
  36.  
  37. keyboard::~keyboard()
  38. {
  39. tcsetattr(0, TCSANOW, &initial_settings);
  40. }
  41.  
  42. int main()
  43. {
  44. keyboard my_board; //essentially removes line buffer
  45. string pass="";
  46. char c=' ';
  47. cout << "Testing passwords, enter a string to passwordize\n";
  48.  
  49. while(c != '\n')
  50. {
  51. c = getchar();
  52. if(c != '\n')
  53. {
  54. if(c == '\b') //doesn't like this line
  55. {
  56. pass=pass.substr(0,pass.size()-1);
  57. cout << "\010 \010" << flush;
  58. }
  59. else
  60. {
  61. pass.push_back(c);
  62. cout << "*" << flush;
  63. }
  64. }
  65. }
  66.  
  67. my_board.~keyboard(); //reverts to normal line buffering
  68. cout << "\nYou entered: \"" << pass << "\"" << endl;
  69.  
  70. return 0;
  71. }

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
Ps. 121

Makes it easier on everyone: http://www.daniweb.com/forums/thread78223.html

AJAX, PHP, C#, C++, JAVA
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 16,163
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1558
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: how do I read backspace (getchar)?

 
0
  #2
Mar 8th, 2008
>>Also, I have no doubt there are better ways to emulate getch(), if you know of one please point me in the right direction.

Its called curses.
My NewYear's resolution is to lose weight.

Weight Lost since 1 Jan 2010: 8.7 lbs
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 111
Reputation: jesseb07 is on a distinguished road 
Solved Threads: 15
jesseb07's Avatar
jesseb07 jesseb07 is offline Offline
Junior Poster

Re: how do I read backspace (getchar)?

 
0
  #3
Mar 8th, 2008
hmmm... yes that does the job (and may look a little neater), but it clears the screen and only outputs things from the function until the program ends, that is not something I want to happen when I actually implement the function in the real program, unless there is an option for it not to do that. It also seems to again not register '\b', so I still have my original problem. code follows.

  1. #include <cstring>
  2. #include <iostream>
  3. #include <ncurses.h>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. initscr();
  9. noecho();
  10. string pass="";
  11. char c=' ';
  12. cout << "Testing password stuff, enter a string to passwordize1\n" << flush;
  13.  
  14. while(c != '\n')
  15. {
  16. c = getch();
  17. if(c != '\n')
  18. {
  19. if(c == '\b')
  20. {
  21. pass=pass.substr(0,pass.size()-1);
  22. cout << "\010 \010" << flush;
  23. }
  24. else
  25. {
  26. pass.push_back(c);
  27. cout << "*" << flush;
  28. }
  29. }
  30. }
  31.  
  32. endwin();
  33. cout << "\nYou entered: \"" << pass << "\"" << endl;
  34.  
  35. return 0;
  36. }
Ps. 121

Makes it easier on everyone: http://www.daniweb.com/forums/thread78223.html

AJAX, PHP, C#, C++, JAVA
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 111
Reputation: jesseb07 is on a distinguished road 
Solved Threads: 15
jesseb07's Avatar
jesseb07 jesseb07 is offline Offline
Junior Poster

Re: how do I read backspace (getchar)?

 
0
  #4
Mar 8th, 2008
I got it to read backspaces, keypad() does that trick. I still wish there was another way to do it while still inside the terminal. That just confirms the things I heard that the terminal intercepts the backspace before it can get to the program (not sure why, but it seems that way). Anywho, thanks for the nudge Ancient Dragon, I think I can still make it look/do the things I would like. For sake of completeness on anyone following this thread, here's the finished code:

  1. #include <cstring>
  2. #include <iostream>
  3. #include <ncurses.h>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. initscr();
  9. noecho();
  10. keypad(stdscr, TRUE);
  11. string pass="";
  12. int c=0;
  13. cout << "Testing password stuff, enter a string to passwordize1\n" << flush;
  14.  
  15. while(c != '\n')
  16. {
  17. c = getch();
  18. if(c != '\n')
  19. {
  20. if(c == KEY_BACKSPACE)
  21. {
  22. pass=pass.substr(0,pass.size()-1);
  23. cout << "\010 \010" << flush;
  24. }
  25. else
  26. {
  27. pass.push_back(c);
  28. cout << "*" << flush;
  29. }
  30. }
  31. }
  32.  
  33. endwin();
  34. cout << "\nYou entered: \"" << pass << "\"" << endl;
  35.  
  36. return 0;
  37. }
Ps. 121

Makes it easier on everyone: http://www.daniweb.com/forums/thread78223.html

AJAX, PHP, C#, C++, JAVA
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum


Views: 5517 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC