942,786 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 9718
  • C++ RSS
Mar 8th, 2008
0

how do I read backspace (getchar)?

Expand Post »
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:

C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 76
Solved Threads: 15
Junior Poster
jesseb07 is offline Offline
111 posts
since Dec 2006
Mar 8th, 2008
0

Re: how do I read backspace (getchar)?

>>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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5591
Solved Threads: 2280
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,927 posts
since Aug 2005
Mar 8th, 2008
0

Re: how do I read backspace (getchar)?

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.

C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 76
Solved Threads: 15
Junior Poster
jesseb07 is offline Offline
111 posts
since Dec 2006
Mar 8th, 2008
0

Re: how do I read backspace (getchar)?

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:

C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 76
Solved Threads: 15
Junior Poster
jesseb07 is offline Offline
111 posts
since Dec 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Write a C++ program, I need help
Next Thread in C++ Forum Timeline: I don't know





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC