Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 2nd, 2006, 3:16 AM   #1
shoeyfighter
Newbie
 
Join Date: Oct 2006
Posts: 10
Rep Power: 0 shoeyfighter is on a distinguished road
Arrow Reading character input into an array (raw mode)

Hello. I am having an issue with a program I am creating. Basically, it is a simply UDP chat client/server, that reads input from stdin in raw mode.

My issue is that I need to keep track of what has been typed, as I am reading character by character. Here is my code:

Note: char input[256];

//if the input is done, then reinitialize
    if(inputdone) {

    //clear the input array
    memset(input, '\0', sizeof(input));

    //reset c
    c = 0;

    //reset inputdone
    inputdone = 0;
}

//get a char
input[c] = getchar();

//if it is a newline, then they are done typing
if(input[c] == '\n') {

    //get rid of the newline
    input[c] = '\0';

    //set inputdone to true
    inputdone = 1;

} else if(input[c] == '\b') {

//HELP HERE

    //clear the current input
    input[c] = '\0';

    //it is a backspace so go backwards
    c--;
    if(c <= 0) c = 0;

    //clear whatever used to be here
    input[c] = '\0';

} else {
    //increment the array
    c++;

    //make sure it doesnt go past the size of input
    if(c > 256) c = 256;
}

This code is all tucked into a while(1) that constantly prints out the contents of input (with a bunch of backspaces first to clear the previous printf). Remember, it is in raw mode.

Basically, when I run this code, the cursor goes back successfully, but the character still shows.

For example:

If I type "abcd", then hit backspace, it will still say "abcd" but the cursor will now be on "c". Does this make sense? Thanks in advance.
shoeyfighter is offline   Reply With Quote
Old Nov 2nd, 2006, 4:37 AM   #2
Game_Ender
Professional Programmer
 
Game_Ender's Avatar
 
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3 Game_Ender is on a distinguished road
Have you tried printing over the character? You could trying going back one char, printing a space, then stepping back again.
Game_Ender is offline   Reply With Quote
Old Nov 2nd, 2006, 2:13 PM   #3
shoeyfighter
Newbie
 
Join Date: Oct 2006
Posts: 10
Rep Power: 0 shoeyfighter is on a distinguished road
Thumbs up

Ah, thanks for the suggesstion.

It required a little more fiddling than JUST that, but it got me on the right track.

Thanks.
shoeyfighter is offline   Reply With Quote
Old Nov 2nd, 2006, 4:49 PM   #4
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
Wouldn't it be way easier to use the fgets function?
Polyphemus_ is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
relation between array and pointer n00b C++ 6 Oct 12th, 2006 4:38 PM
incrementing character array elements value n00b C++ 7 Jun 24th, 2006 4:44 AM
Change the size of a character array dynamically? bivhitscar C 23 Nov 5th, 2005 6:55 AM
Installing IPB 2.03 bh4575 Other Web Development Languages 0 Apr 23rd, 2005 3:36 AM
Simple question: Easiest way to search through a character array willz99ta C++ 6 Mar 26th, 2005 3:15 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:03 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC