Advertisement

Console-like Backspace routine ?

Started by December 09, 2003 03:35 AM
9 comments, last by Living Monstrosity 21 years, 2 months ago
Hi, i am currently making a program needing a one-line console. Everything works fine except the backspace routine. Here is what i have now:

				if(keys[VK_BACK]){
					keys[VK_BACK]=false;
					char dummy[]="";
					int l=strlen(g_verb);
					if(l>0){
						char dummy[]="";
						for(int c=0;c<l-1;c++){
							char dummy2[]="";
							wsprintf(dummy2,"%c",g_verb[c]);
							strcat(dummy,dummy2);
						};
						strcpy(g_verb,dummy);
					};
				};
It might look a bit messy, but i''ve been trying to get this right for about an hour. The problem is, right now, that it works fine when the string g_verb is 4 or less characters, when its larger, the string is cropped to 4 characters (the first 4). Has it got something to do with the pointer size (4), or something else. Please let me know, thanks in advance.
- growl -
you wouldn''t happen to know an easy way to stop the console from repeating the characters from the keyboard? I''ve been trying to solve this for some time and it''s still repeating shit :/
www.prsoftware.de
Advertisement
That''s pretty simple:

keys[WHATEVER]=false;

Now the input will be just like a normal text editor, firts you''ll get one character , then nothing for some time, and the they will keep coming after eachother till you release the key.
- growl -
By the way, i get access violation with my backspace routing, but no error box.
- growl -
firstly, if ur in C++ then use std::string, it''ll be easier and whenever you need to specify a c-string just call std::string::c_str() to get a const c-string used by the std::string object.

secondly the exception may be caused by running out of space or something? are you using dynamic memory? (malloc/free or new/new[]/delete/delete[]).

for the VK_BACK the string doesn''t need reallocating since your only reducing the space required but if increasing a larger string may be nessessary.

if(keys[VK_BACK]){	keys[VK_BACK]=false;	int l = strlen(g_verb); // returns size of string and subsequently the last character too!	g_verb[l] = ''/0''; // set the old last char to the null terminating character};


the memory allocation will not be affected by the above

< krysole || krysollix >
sleep, caffeine for the weak minded
Thanks alot, however, you did do a few things wrong:
First of all, the last character isn''t the string''s length, it''s the string''s minus one (0-indexed).
The second is that a NULL character isn''t ''/0'' but ''\0''.

so:
g_verb[l-1] = ''\0'';

This works fine, thanks.

I have one question left, as you''re setting the \0 character one step back (so you have 2 of them, isn''t the last character actually still allocated for the string. Or means a ''\0'' character also ''not in use, so you can allocate it for whatever you want''. This because the strlen funtion simply counts how many characters there are before it finds a ''\0'' character.
- growl -
Advertisement
heh, I did it like you said it works for one line,but I need it in a complicated edit box class, not that easy :/
www.prsoftware.de
You could, when a user presses enter, add a different character to the string ( like (char)1 ) and then, when you display it, begin a new line on every (char)1 character. This way, you can also backspace new lines away.
- growl -
Yes the extra null terminator would still be allocated.
With standard gaming systems having 512Mb RAM.. I don''t think you need to worry about a single byte. Reallocating would probably hurt more than help.
Ok, and I just wondered about that as I am always pretty curious what commands actually do in detail.
I now have a serious wrist injury by playing guitar 24/7 for 3 weeks so i won't be able to use this usefull information the next few weeks :'(

[edited by - Living Monstrosity on December 11, 2003 3:00:24 PM]
- growl -

This topic is closed to new replies.

Advertisement