Thread: clearing last input on display

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    13

    clearing last input on display

    Is it possible to clear last input on display without clearing hall display using system("cls") or fflush(stdout)?I want to make timer so i need to delete last input to show new time...

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    fflush(stdout) will not clear the display, it only sends what's in the output buffer to screen. Generally to do screen tricks like this you need a screen library like curses/ncurses. A workaround is to print the backspace character to back up the cursor over the printed time, print the time then call fflush(stdout). You can't put a newline after the time though. Not sure how portable that is either.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    No portable way.
    In the same line you can try
    Code:
    putchar('\b');
    for all line-chars and then put space(s)
    Code:
    putchar(' ');
    There are escape-sequences, which can help you.

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    13
    Thanks. What I have to change in this code to make it working properly?After printing 10 it prints 90.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    main () {
         time_t b,e;
         e=time(NULL);
         b=e;
       printf("\nTimer:   ");
         while(time(NULL)!=(e+20)) {
               if(b!=time(NULL)){
                                if(((e+20)-time(NULL))>=9){printf("\b");}
                                 printf("\b%d",(e+20)-time(NULL));
                                 b++;
                }                 }
         system("pause");
         }

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    For starters, you need to properly declare main and return an integer at the end of it (read this link). You also need to work on formatting your code (don't cram multiple statements onto one line, and put some space around your operators), picking better variable names and writing useful comments so that the rest of the world can understand what your code is trying to do.

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    13
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main () {
         time_t b,e;
         e=time(NULL);  // current time
         b=e;
         printf("\nTimer:   ");
         while(time(NULL)!=(e+20)) {  // e+20 is end time
               if(b!=time(NULL)){     //usage of b is to prevent printing after each pass through the loop
                                if(e+20-time(NULL)>=9)printf("\b");
                                             
                                printf("\b%d",e+20-time(NULL));
                                b++;
                }                 }
         system("pause");
         return 0;
         }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Clearing the input stream
    By abh!shek in forum C Programming
    Replies: 10
    Last Post: 05-18-2008, 03:50 PM
  2. Clearing the input buffer
    By caduardo21 in forum C Programming
    Replies: 1
    Last Post: 05-14-2005, 08:40 PM
  3. Clearing the Input buffer
    By Brain Cell in forum C Programming
    Replies: 5
    Last Post: 03-21-2004, 12:08 PM
  4. clearing input stream
    By Sub in forum C++ Programming
    Replies: 2
    Last Post: 01-21-2002, 08:59 PM
  5. Clearing out an input line
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-22-2001, 12:27 PM