line editor problem

Jan 7, 2009 at 4:41am
Hi,

I've uploaded the files to pastebin.com

dtio.h (http://pastebin.com/m3cbd209a)
dtio.c (http://pastebin.com/mcea9743)
a2test.c (http://pastebin.com/m6e249679)

I'm trying to program a line editor but I've run into a problem that I'm totally stuck on.

a2test.c is the test main that I have to pass. My program fails when I follow the instructions on this line...

Press Delete Twice, 7, 8, 9, Enter

When I press the key 9 I get a heap error. I've narrowed the problem down to the for loop block starting at line 397 in dtio.c

I've even managed to solve the problem by making the following change on line 397...

for (i = linePos; i < strlen(theLine) - 2; i++){

but when I do that (above) something else in the test main fails. This time when I follow the instructions...

Press End, Backspace Twice, 1, 2, 3, Enter

the string is not correct.

Now my problem is I can't figure out a way to make both of those instructions to give me the correct results...

Press End, Backspace Twice, 1, 2, 3, Enter
Press Delete Twice, 7, 8, 9, Enter

I've tried various things but I just can't figure it out. If someone could have a look and point me in the right direction I would really appreciate it.

Thanks
Jan 7, 2009 at 6:48pm
dtio.c Line 397:
1
2
for (i = linePos; i < strlen(theLine); i++){
 temp = theLine[i+1];

The problem with this code is that when i is strlen(theLine)-1 that is fine. But when you add +1 to it this takes i to 1 past the end of the array and into unknown memory.

for (i = linePos; i < strlen(theLine)-1; i++) should work.

Another question. Why are you using char* instead of the string type?
Jan 8, 2009 at 12:00am
Zaita, first of all thanks for posting.

The fix you suggested fixes my initial problem, that being...

Press Delete Twice, 7, 8, 9, Enter now works but it breaks

Press End, Backspace Twice, 1, 2, 3, Enter and there in lies my whole problem. I explained this in the OP

I'm using char * because that somebody else started this and I'm just trying to finish it up. So he started it with have char * so I have to continue using it
Jan 8, 2009 at 12:29am
Hmmm ok. Can you tell me the result you get for End,Backspace command?
Jan 8, 2009 at 1:11am
When using for (i = linePos; i < strlen(theLine)-1; i++)

for the End, Backspace command...

I get the string...

AbCdefg12JKLmnopqrstuv

when I should be getting...

AbCdefg12JKLmnopqrstuv12

So the last two letters/digits are missing.
Jan 8, 2009 at 7:09pm
Have you solved the issue?
Topic archived. No new replies allowed.