roger569 Posted August 14, 2013 at 03:22 AM Report #521680 Posted August 14, 2013 at 03:22 AM (edited) Queria fazer um ciclo que so parasse após carregarmos numa tecla, por exemplo: while("Qualquer coisa que eu nao sei"){ printf("So para depois de carregarmos numa tecla qualquer"); } Alguem consegue ajudar? Edited August 14, 2013 at 04:44 AM by roger569
nelsonr Posted August 14, 2013 at 07:18 AM Report #521682 Posted August 14, 2013 at 07:18 AM Boas, vê se isto ajuda: http://stackoverflow.com/questions/13547471/c-programming-check-if-key-pressed-without-stopping-program
Skun Fly Posted August 14, 2013 at 11:19 PM Report #521778 Posted August 14, 2013 at 11:19 PM Seria algo assim? #include <stdio.h> int main() { while(!getchar()) //ciclo não acaba enquanto não for pressionada uma tecla printf("Para parar o ciclo clique numa tecla!"); return 0; }
thoga31 Posted August 15, 2013 at 03:52 PM Report #521806 Posted August 15, 2013 at 03:52 PM A função getchar pára até que seja dado um input. Lê bem o link que te forneceram - falaram em getchar ou em kbhit? You can use kbhit() to check if a key is pressed: #include <stdio.h> #include <conio.h> /* getch() and kbhit() */ int main() { char c; for(;{ printf("hello\n"); if(kbhit()){ c = getch(); printf("%c\n", c); } } return 0; } Knowledge is free!
pwseo Posted August 16, 2013 at 10:57 AM Report #521859 Posted August 16, 2013 at 10:57 AM roger569, Se estiveres a utilizar Windows, a solução reforçada pelo thoga31 é a correcta. Se por acaso o programa precisa de funcionar em Linux, terás que investigar sobre como utilizar as funções tcgetattr e tcsetattr (header <termios.h>), ou então terás que te aventurar com a lib ncurses. Tudo isto acontece porque o que estás a tentar fazer não diz respeito à linguagem C mas sim ao sistema no qual o programa corre. Por esse motivo, diferentes sistemas têm diferentes formas de aceitar input do utilizador (e diferentes formas de configurarmos esse processo). 1 Report
HappyHippyHippo Posted August 16, 2013 at 02:08 PM Report #521869 Posted August 16, 2013 at 02:08 PM como só foram apresentadas as seguintes soluções : - ou tens o buffered and blocking input reading do scanf - usa o ncurses ou os conio.h/<termios.h> (dependendo do SO) eu apresento uma intermédia (código para linux mas fácil de portar ou usar para win32 (dependendo do compilador usado) : // nao esquecer de linkar a biblioteca "pthread" // // exemplo : gcc -o test test.c -lpthread #include <stdio.h> #include <stdlib.h> #include <pthread.h> #define SECS 3 #define STEP 1 #define RUN 1 #define STOP 0 void * proc(void * data) { int * run = (int *) data; int count = 0; while (*run == RUN) { if (count == 0) printf("doing stuff for %d seconds ...\n", SECS); sleep(STEP); count = count >= SECS - STEP ? 0 : count + STEP; } pthread_exit(NULL); } int main() { pthread_t thread; int run = RUN; printf("starting worker thread ...\n"); pthread_create(&thread, NULL, proc, &run); printf("waiting for input ...\n"); getchar(); printf("thread signal to stop\n"); run = STOP; printf("waiting for thread to stop ...\n"); pthread_join(thread, NULL); return 0; } podes testar que o thread auxiliar fica a ser executado enquanto o scanf bloqueia o thread principal. continuas a ter de carregar esplicitamente na tecla de ENTER, mas como disse, é uma solução intermédia. IRC : sim, é algo que ainda existe >> #p@p Portugol Plus
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now