0

The point of this program is to print "Hello World" 1000 at a certain speed. The user can press 'f' key to speed up the print or 's' key to slow down the speed and 'x' key to end the program. I need to use getch() so that it does a non-blocking read form the keyboard. I've tried this, but I can't get my head around how getch() works...

#include<stdio.h>
#include<ncurses.h>

int main()
{
    int speed = 5000000, i;
    char keypress;
    for (i=0; i<1000; i++)
    {
            printf("Hello World!\n");

    keypress = getch();
    switch(keypress)
            {
                    case 'f':
                    speed+= 100000;
                    break;
                    case 's':
                    speed-= 100000;
                    break;
                    case 'x':
                    return;
                    break;
            }
    usleep(speed);
    printf("__________________________________");
    }
    return 0;

}

M.Ser
  • 9
  • 4
  • I tried duplicating it, but I can't find a way to implement it in my code... I'm fairly new to getch(), is there anyway you might be able to be a little more specific to my case? I'd greatly appreciate it – M.Ser Oct 22 '15 at 01:44
  • 1
    Are you on windows or linux? There are more examples: [here](http://stackoverflow.com/questions/448944/c-non-blocking-keyboard-input), [here](http://stackoverflow.com/questions/20588002/nonblocking-get-character), and [here](http://stackoverflow.com/questions/717572/how-do-you-do-non-blocking-console-i-o-on-linux-in-c). Search for 'non-blocking keyboard input' and such. – Kenney Oct 22 '15 at 01:57
  • As the referenced question&answer states, in order to use `getch` you need to first initialize the curses library with `initscr` or `newterm`... – Chris Dodd Oct 22 '15 at 02:33

0 Answers0