Thread: How do you move up a line?

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    2

    How do you move up a line?

    Here's my code:

    Code:
    // Word per minute counter by Pinny Berlin ([email protected])
    // Do whatever you want with the code.
    
    #include <stdio.h>
    #include <time.h>
    #include <termios.h> // for mygetch function
    #include <unistd.h>  // for mygetch function
    
    int mygetch( ) {
    
    /* This function accepts and returns an inputed character immediatly (even before the user presses enter). Function written by VvV. */
    
      struct termios oldt,
                     newt;
      int            ch;
      tcgetattr( STDIN_FILENO, &oldt );
      newt = oldt;
      newt.c_lflag &= ~( ICANON | ECHO );
      tcsetattr( STDIN_FILENO, TCSANOW, &newt );
      ch = getchar();
      tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
      return ch;
    }
    
    void secPause() {
    
    // This function creates a ~1 second pause.
    
      time_t start, current;
      start = time(NULL);
      do {
        printf(" \b");
        current = time(NULL);
      } while (difftime(current, start) < 1);
    }
    
    int main ()
    {
    
    // The program counts how many words you type on average per minute.
    
      char c, buffer;
      time_t start, current;
      int spaces = 0;
      printf("\nStart typing in... ");
      secPause();
      printf("3... ");
      secPause();
      printf("2... ");
      secPause();
      printf ("1  NOW!\n---> ");
    
      start = time(NULL);
    
      do {
        c = mygetch(); // Inputs a single keystroke
        if (c == ' ') spaces++;
        printf("%c", c);
        printf("   Total words per minute: %3d!", 60 / difftime(current, start) * spaces);
        printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
        current = time(NULL);
      } while (1);
    
      printf("\n");
      return 0;
    }
    Compiling this and run it (press ctrl-c to quit). You'll see that at the end of every line, the text "Total words per minute: 77" remains. '/b' goes back a space but it doesn't go back a line. How do you go back a line?

    Thanks in advance.

    P.S. Anyone know why my secPause funtion doesn't work out if I take out the printf line? I can't figure it out.
    Last edited by epb613; 05-31-2005 at 01:05 PM.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    \r

    dwk

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    2
    ty. Any idea about the secPause function?

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    FAQ > How do I... (Level 2) > Sleeping
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ASM beginner gets 24 compiler errors on hello world...
    By Desolation in forum Tech Board
    Replies: 12
    Last Post: 06-16-2007, 10:21 PM
  2. Replies: 19
    Last Post: 05-30-2007, 05:47 PM
  3. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  4. SSCANF help
    By mattz in forum C Programming
    Replies: 7
    Last Post: 12-10-2001, 04:53 PM
  5. Validating the contents of a char buffer
    By mattz in forum C Programming
    Replies: 3
    Last Post: 12-09-2001, 06:21 PM