Thread: Problem with BackSpace character

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    132

    Problem with BackSpace character

    Hi, everyone. I am trying to write the following program: initially, all the fields which will be filled by the user appears on the screen; then, the first field to be filled is name. So, before using scanf to read in the name, I want to make the cursor go back to beggining of the underlines that follow the name field:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int i;
        char name[25];
    
        printf("USER INFO:\n");
        printf("Name: _________________________"); //25 underlines
        printf("  Age: __"); //2 underlines
        printf("  Address: _________________________"); //25 underlines
        printf("    Phone 1: ________"); //8 underlines
        for(i = 1 ; i <= 81  ; i++)
        {
            printf("%c", 8);
        }
        scanf("%s", name);
    
        return 0;
    }
    So, I am using the backspace character to go back in the text - however, it stops at the begging of the line it started going back, and even when I add more backspaces, it still doesn't go back to the line above - so, is it possible to accomplish such task?

    Thanks in advance!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You will need some console specific functions for that. This will depend on your OS/compiler/3rd party libraries (like ncurses).


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    132
    I see, thanks.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    In Windows, you can use the windows API function named SetConsoleCursorPosition().

    So each field would have a row and column value saved in a COORD() struct that SetConsoleCursorPosition() would use.

    Of course, you'll need to include the <windows.h> header in your program. I'd use a nested COORD's, in the struct, or simple descriptive variables, to hold the values.

    Could be just as simple as:

    int name_x=2, name_y=1;

    So the first space in that field would be row 1, column 2.

    Generally, coming from DOS and Turbo C, I have used the conio.h header a lot for this kind of programming, but conio.h is not a standard header, and unlikely to ever become one (it's quite similar to ncurses).

    The Windows API functions for this, are much more "standard" nowadays.

    Give that a shot. If this has your head spinning, I'll help you sort out the kinks to get this working. It's not as hard as it may sound, really.

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    You may find some value with adrianxw's console tutorial-Part 2. Additionally, a very good source for windows programming is Forger's Win32Tutorial.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    A simple example, placing a puzzle on a certain row and column



    Code:
    /* shows how to use SetConsoleCursorPosition(), in Windows */
    
    #include <stdio.h>
    #include <windows.h>
    
    void Gotoxy(int x, int y); 
    void showIt(void);
    int a[81] = {
    {1,2,3,4,5,6,7,8,9},
    {4,5,6,7,8,9,1,2,3},
    {7,8,9,1,2,3,4,5,6},
    {0,0,0,0,0,0,0,0,0},
    {0,0,0,0,0,0,0,0,0},
    {0,0,0,0,0,0,0,0,0},
    {2,1,4,3,6,5,8,9,7},
    {3,6,5,8,9,7,2,1,4},
    {8,9,7,4,1,2,3,6,5}
    };
    
    int main(void) {
      showIt();
    
      return 0;
    }
    
    void Gotoxy(int x, int y) {
       COORD coord;
       coord.X = x;
       coord.Y = y;
       SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
    }
    void showIt(void)  {
      int i;
      Gotoxy(10,5);
      for(i = 0; i < 81; i+=9)  {
         printf("  | %d%d%d | %d%d%d | %d%d%d |\n", a[i+0],a[i+1],a[i+2],a[i+3],a[i+4],a[i+5],a[i+6],a[i+7],a[i+8]);
         if(i == 18 || i == 45) printf("  +-----+-----+-----+\n");
      }
    }
    Last edited by Adak; 09-07-2011 at 01:05 AM.

  7. #7
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Adak,

    I would like to tell you that your examples have always helped me out the most. Thank you for your time.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Thank you Andrew.

    You've certainly been very generous with your own time, helping out in the forum. I'm sure the OP will find the links you posted up, very helpful.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem checking backspace and enter.
    By programmer1234 in forum C Programming
    Replies: 2
    Last Post: 01-18-2011, 11:36 AM
  2. Removing a newline character using backspace
    By sambolu in forum C Programming
    Replies: 3
    Last Post: 12-29-2010, 12:16 PM
  3. Replies: 4
    Last Post: 08-27-2007, 11:44 AM
  4. Backspace
    By Necrofear in forum C++ Programming
    Replies: 13
    Last Post: 05-11-2006, 01:16 PM
  5. Richedit backspace character????
    By gbaker in forum Windows Programming
    Replies: 0
    Last Post: 08-12-2003, 11:38 AM