Hi.
Haven't posted here in a while. Anyway , using a basic console app, how would I say the following :
CheersCode:If backspace is pressed, delete previous character.
Hi.
Haven't posted here in a while. Anyway , using a basic console app, how would I say the following :
CheersCode:If backspace is pressed, delete previous character.
You can read the answer to this in the FAQ.
Last edited by whiteflags; 05-11-2006 at 10:56 AM.
Oh, sorry didn't see this.
Ta
Oh one more question : How do I flush strings ?
(I think thats the right terminology.)
guess it's notOriginally Posted by Necrofear
do you mean
KurtCode:string s = "something"; s = ""; // now it's empty
Strings don't need to be flushed I think, but you can erase them withEDIT: Dammit, Kurt beat me.Code:string.erase();
Last edited by whiteflags; 05-11-2006 at 11:55 AM.
Yes ! Thank you, thats exactly what I meant.
But using citizen's method, how does it know which string to erase ?
It won't. In practice, "string" in my example should be the actual name of a string object.
use like this
KurtCode:string s = "something"; s.erase();
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 :
Where would I put a loop similar to the "While ch != '\b" loop ?Code:cout<<"\nEnter Password : "; (input = getch()); while(isalnum(input)) { password += input; cout<<"*"; (input = getch()); }
Last edited by Necrofear; 05-11-2006 at 12:33 PM.
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'; }
I think I get that, but how would I implement that into my current code ?
And what is the : s.size() for ?
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.
>> 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.