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.