4
#include <iostream>
#include <conio.h>

int main(){

    while(true){
        printf("Hello World!\n");
    }

    return 0;
}

The above program will print "Hello World" endlessly. I want that as soon the user presses the "T" key on the keyboard, the program terminates. Any clue on how to do this.......

If I do it like

#include <iostream>
#include <conio.h>


int main(){

    char key;
    while(true){
       printf("Hello World!\n");
        key = getch();
        if(key=='T' || key=='t'){
            break;
        }
    }

return 0;
}

Then the program will always wait for the user to press a key. I want that the program continues executing without pausing, and as soon as user presses any specific key, then the program terminates.

By the way my operating system is linux (debian) and I'm using gcc.

Melika
  • 411
  • 1
  • 5
  • 9
  • 1
    You have to do a thread which wait user input and on T or t exit the program with the `exit` call to stop the whole process. – Sylvain Bugat Mar 26 '15 at 22:32
  • non-blocking `read()` from stdin? – EOF Mar 26 '15 at 22:33
  • 2
    I don't think you can do that using standard library functions. You can use platform specific libraries. You can use [`conio.h`](http://www.programmingsimplified.com/c/conio.h) on windows and [ncurses](http://invisible-island.net/ncurses/) on Linux. – R Sahu Mar 26 '15 at 22:38
  • there's always a way to terminate the command.. ctlr+C – GorvGoyl Mar 26 '15 at 22:39
  • possible duplicate of [Cin without waiting for input?](http://stackoverflow.com/questions/27582493/cin-without-waiting-for-input) – Christophe Mar 26 '15 at 22:56
  • @Christophe: You cannot use `std::cin` in C. This sounds like a duplicate of http://stackoverflow.com/q/1276029/103167 but despite the name, that one turns out to be about tty echo. – Ben Voigt Mar 26 '15 at 23:00
  • @BenVoigt Sorry I didn't notice it was only C. But the problem is the same: getch() can be buffered. – Christophe Mar 26 '15 at 23:09
  • Why use `iostream`? Did you mean `stdio.h`? – Spikatrix Mar 27 '15 at 02:42

3 Answers3

6

conio is Windows (OP tagged this as Linux). The question is frequently asked, usually answered pointing to termios, e.g.,

On the other hand, ncurses provides functions which are useful -- but unless you use filter, the screen will be cleared. Here are useful links for functions to consider:

By setting a short timeout (say 20 milliseconds), the program will respond faster than anyone's reaction time, and use little CPU.

Here is a revised program illustrating filter:

#include <ncurses.h>
#include <stdlib.h>

int
main(int argc, char **argv)
{
    int ch = 0;
    int n;
    int code;

    filter();
    initscr();
    timeout(20);
    for (;;) {
        move(0, 0);
        for (n = 1; n < argc; ++n) {
            printw("%s ", argv[n]);
        }
        printw("[y/n] ");
        clrtoeol();
        ch = getch();
        if (ch == 'Y' || ch == 'y') {
            code = EXIT_SUCCESS;
            break;
        } else if (ch == 'N' || ch == 'n') {
            code = EXIT_FAILURE;
            break;
        }
    }
    endwin();
    return code;
}

(Checking for y/n seems more useful than checking for "t" -- feel free to customize).

Community
  • 1
  • 1
Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
0

You can do a boolean checking before getting user input char getch() . this link explains well about it To see if user press a key

Community
  • 1
  • 1
Kyaw
  • 117
  • 1
  • 7
0

If you don't want to wait for keyboard input, there is unfortunately no standard way to proceed.

If your conio.h supports kbhit() just use it. Otherwise, for any linux and posix, you can look here for Morgan Mattews's code to provide the same functionality.

Your body of your loop would then look like this:

    ...
    if (kbhit()) {
        key = getch();
        if(key=='T' || key=='t'){
            break;
        }
    }
Christophe
  • 68,716
  • 7
  • 72
  • 138
  • 1
    POSIX terminal I/O is actually a standard -- http://pubs.opengroup.org/onlinepubs/000095399/basedefs/xbd_chap11.html – Thomas Dickey Mar 26 '15 at 23:39
  • @ThomasDickey yes, fully true (the solution of Morgan Mattews is fully based on the termios). When I said "no standard way", I meant in fact the C standard library, because of its portability accross OS. – Christophe Mar 27 '15 at 00:07