Thread: ascii backspace, K&R book exercises

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    4

    Question ascii backspace, K&R book exercises

    I am going through the K&R book (incredible book!).

    Exercise 1-10 is to mirror input, except to replace backspace with "\b", tab with "\t", and newline with "\n". You would see a literal backslash-b for each backspace in input.

    I am on windows NT with gcc 2.97, and I can't seem to read the backspaces properly, to mirror them as "\b". The other tab and nl bits work fine. I tried using the ascii code of 8 and 127 (DEL), in addition to '\b', without any better results.

    code excerpt:

    ...
    while ( (ch=getchar()) !=EOF){
    if (ch =='\b'){
    putchar('\');
    putchar('b');
    }
    ...

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    >putchar('\');
    putchar('\\');
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Xsquared beat me one problem, but the other is that backspace will probably not actually be seen in your program, because of buffered IO. In short, when you type your line of text, it is buffered before being passed to your program, if you hit backspace, the OS will simply remove the last character from the buffer for you.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    4

    backslash backspace

    Whoops; I *did* escape the backslash in my original, non-working code. This was not reflected in my post (not escaping it prevents it from compiling at all, as I learned in this exercise).

    Thank you for a swift reply!

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    4

    buffered IO

    Thanks, Hammer. That must be it. Is there a way to flush or unbuffer my STDIN fro the program?

    Even if there isn't, your explanation allows me to proceed guilt-free to the next exercises.

  6. #6
    Registered User
    Join Date
    Mar 2003
    Posts
    4

    fflush(stdin)

    The animated hint to try fflush(stdin) did not seem to work, either inside or outside the while loop. Here is what I have:

    Code:
    #include <stdio.h>
    
    main(){
      int ch;
      fflush(stdin);
    
      while( (ch=getchar() ) != EOF){
    
       if (ch == '\b'){
         putchar('\\');
         putchar('b');
        }
        if (ch == '\t'){
          putchar('\\');
          putchar('t');
        }
        putchar(ch);
      }
    }
    I see the [code] tags swallow the escaping basckslashes
    Last edited by jjohhn; 03-16-2003 at 05:49 PM.

  7. #7
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    In order to use unbuffered input, you will have to #include <conio.h> and use getch( ) instead of getchar( ).
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  8. #8
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807

    Re: fflush(stdin)

    The animated hint to try fflush(stdin)
    The animated hint, it was to show you that you shouldn't use fflush(stdin), let the gif end before you post

    Search the faq to see how to flush input buffer

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>The animated hint to try fflush(stdin)
    Read this , which will help you understand fflush(stdin), but it will not solve your problem.

    To get unbuffered input, as mentioned use getch(), examples here
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    C Beginner()
    Join Date
    Oct 2004
    Posts
    22

    One other thing

    From seeing the posts before mine this question came into my mind: why would Kernighan & Ritchie put exercise 1-10 at that point of their book if the "\b part" of it would not work? May it be that it worked in the computers/OSs they used when they wrote the book?

    Put in another way, why should it be necessary to flush standard input in order to get the program to do all the tasks it is told to do when those concepts were not discussed at that point in the book?

    Read correctly, i'm grateful to all of you who shared your knowledge but i think that it is important to answer these kind of questions with the knowledge you got by reading up to that part of the book. Sometimes, invoking functions that were not discussed might confuse newbies (such as myself.)

    Thanks to all, great forum.

  11. #11
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Great work on bumping a nearly three year old thread. Have a look at the forum guidelines, particularly nimber five.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My book recommendations for rank beginners ...
    By snakum in forum C++ Programming
    Replies: 4
    Last Post: 08-21-2002, 10:38 AM
  2. Newbie - MFC code from a book in VC++.Net
    By Guardian in forum Windows Programming
    Replies: 2
    Last Post: 04-27-2002, 07:17 PM
  3. K&R for C++ ?
    By Barjor in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 04-09-2002, 12:02 PM
  4. Looking for an unusual c++ book
    By algruber in forum C++ Programming
    Replies: 13
    Last Post: 01-22-2002, 12:21 PM
  5. C++: Reference Book, GUI, Networking & Beyond
    By kuphryn in forum C++ Programming
    Replies: 4
    Last Post: 11-10-2001, 08:03 PM