1

I want to create a code in C, that runs a loop and breaks the loop when I hit '1' BUT without pressing ENTER after it.

I use the GCC compiler.

example Code:

While-loop has to run always without pause, until I press '1' but without hitting ENTER ...(bin/stty raw)


int c=0;
int leave=0;
system ("/bin/stty raw"); 
while ((leave=getchar()) != '1'){
        c++;
        printf("\n- - while loop nr. %d\n",c);
        sleep(1);
}
system ("/bin/stty cooked");
printf("\n-- end of loop...\n");*
...

dan1st
  • 12,568
  • 8
  • 34
  • 67

1 Answers1

0

This is a C FAQ, answered here: http://c-faq.com/osdep/cbreak.html

Q: How can I read a single character from the keyboard without waiting for the RETURN key? How can I stop characters from being echoed on the screen as they're typed?

Basically, it is system-dependent and library-dependent. There are several possible solutions for various systems listed.

Jens
  • 69,818
  • 15
  • 125
  • 179