Backspace

This is a discussion on Backspace within the C++ Programming forums, part of the General Programming Boards category; Hi. Haven't posted here in a while. Anyway , using a basic console app, how would I say the following ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    183

    Backspace

    Hi.

    Haven't posted here in a while. Anyway , using a basic console app, how would I say the following :

    Code:
    If backspace is pressed, delete previous character.
    Cheers

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,667
    You can read the answer to this in the FAQ.
    Last edited by whiteflags; 05-11-2006 at 10:56 AM.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    183
    Oh, sorry didn't see this.
    Ta

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    183
    Oh one more question : How do I flush strings ?
    (I think thats the right terminology.)

  5. #5
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by Necrofear
    Oh one more question : How do I flush strings ?
    (I think thats the right terminology.)
    guess it's not

    do you mean
    Code:
       string s = "something";   
       s = ""; // now it's empty
    Kurt

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,667
    Strings don't need to be flushed I think, but you can erase them with
    Code:
    string.erase();
    EDIT: Dammit, Kurt beat me.
    Last edited by whiteflags; 05-11-2006 at 11:55 AM.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    183
    Yes ! Thank you, thats exactly what I meant.
    But using citizen's method, how does it know which string to erase ?

  8. #8
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,667
    It won't. In practice, "string" in my example should be the actual name of a string object.

  9. #9
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    use like this
    Code:
    string s = "something";
    s.erase();
    Kurt

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    183
    Ah, clever.
    Thanks for all your help.

    Now I have the string eraseing figured out, I'm gonna go back to the backspace problem. I've read that FAQ, and thought I understood it, but I don't.

    I have this :

    Code:
    cout<<"\nEnter Password :    ";
        (input = getch());
        
        while(isalnum(input))
        {
        password += input;
        cout<<"*";
        (input = getch());
        }
    Where would I put a loop similar to the "While ch != '\b" loop ?
    Last edited by Necrofear; 05-11-2006 at 12:33 PM.

  11. #11
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,667
    getch returns an integer at a time. For as long as the user doesn't press Enter you need to check each value that getch returns to see if it's valid, and if it is you can append it to the string and write an asterisk to the screen.

    When the user presses backspace, it is a special case. You need to ensure that the letter is not put in the string, and reset the users cursor position.
    Code:
    if (ch == '\b' && s.size() > 0)
    {
       cout << "\b \b";
       cout.flush();
       s[s.size()-1] = '\0';
    }

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    183
    I think I get that, but how would I implement that into my current code ?
    And what is the : s.size() for ?

  13. #13
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,667
    I'm assuming you are using strings. size() returns the number of characters in the string, and size()-1 is the last character in that string.

    Um, your while loop is a bit strange, because '\b' is not an alpha-numeric character, so if the user types backspace, you won't enter that loop. You need to change that while condition.

    Code:
    int input;
    string passw;
    // until the user finishes
    while ((input=getch()) != EOF && input !='\n' && input != '\r')
    {
    // check for a backspace
       if (input == '\b' && s.size() > 0)
       {
          cout << "\b \b";
          cout.flush();
          s[s.size()-1] = '\0';
       }  
    // validate and append
       else if(isalum(input))
       {
          cout.put('*');
          passw.append(input);
       }
    }
    Last edited by whiteflags; 05-11-2006 at 01:27 PM.

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> s[s.size()-1] = '\0';

    That code is incorrect. C++ strings don't pay attention to null characters. If you want to erase the last character from a string, you can use s.erase(s.size()-1);.

    BTW, I would use s.clear() to empty a string.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. telnet server, how to program a backspace
    By Mastermosley in forum C# Programming
    Replies: 5
    Last Post: 03-22-2009, 02:14 AM
  2. ascii backspace, K&R book exercises
    By jjohhn in forum C Programming
    Replies: 10
    Last Post: 12-29-2005, 07:33 AM
  3. backspace info
    By kermit in forum Linux Programming
    Replies: 4
    Last Post: 11-28-2003, 01:55 PM
  4. Richedit backspace character????
    By gbaker in forum Windows Programming
    Replies: 0
    Last Post: 08-12-2003, 11:38 AM
  5. Replies: 3
    Last Post: 11-30-2001, 06:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21