View Single Post
Old 07-29-2004, 04:58 AM   #4 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
redhead is on a distinguished road
There is no standard way of activating on key-press which is what you're trying here.
It is very system dependant, thus in order to help you with this specific issue, you need to tell us what compiler/OS you want this program to run with.
Quote:
is it possible to delete a newline character?
something like using the '\b' ?.. this would mean i could use a scanf then printf to delete the newline necessary to complete the scanf.
The '\b' is for bell, the '\r' will return the cursor to position 1 on the line, meaning if you print anything like:
Code:
int counter = 0;
while(counter < 10)
{
    printf(" This is my counter: %d\r", counter++);
    sleep(3);
}
printf("\n");
The line "This is my counter: (number)" will remain on the same line and keep on overwriting previus printed lines, thus only showing current counter number.
The '\r' will in no way affect the scanf() call, since it is bound to STDOUT and not STDIN which scanf() reads from.
So you need to work around stripping the '\n', but this is easy, just read it with scanf(), getline(), fscanf() or whatever and then use strlen() on it later on, like:
Code:
int length = 50;
char line[length];
printf("Enter your text, and terminate with ^D\n\n");
while (fgets(line, length, stdin) > 0)
{
    while(line[strlen(line)] == '\r' || 
          line[strlen(line)] == '\n') 
        line[strlen(line) - 1] = '\0';
    /* 
     * do what you want with line, it is holding the 
     * text written, without <cr> or '\n' in it.
     * It could be a correction to the writer ie:
     *          Your line of text: "He took ione C++ course"
     *          Shouldn't it be  : "He took one C++ course" ?? (Y/N)
     */
}
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001

Last edited by redhead; 07-29-2004 at 05:56 AM.
redhead is offline   Reply With Quote