Newsgroup: it.comp.lang.c
Titolo: Riguardo ai cicli

Come si fa a mandare in loop un ciclo e fermarlo solo quando si preme un
qualsisi tasto?.
In Pascal se ricordo bene si può fare:
repeat
......
until keypressed
In C potrei fare:
 while (ch !='\n')
 .......
ch=getchar();
ma in questo modo il ciclo si interrompe sempre in attesa del carattere.
Bye.

ma in questo modo il ciclo si interrompe sempre in attesa del carattere.
Esatto, ma non solo, getchar() attende il termine della linea corrente
di input per restituire il risultato.


~~~~~~~~~~~~~~~~~
MAINARDI Gianfranco
Burago di Molgora (MI)
Italia
Email : 
main...@rete039.it
ICQ# number : 31334916
~~~~~~~~~~~~~~~~~

, e proviene da
comp.unix.programmer.
Hope that helps,        M.
-- 
Maurizio Loreti                      
http://wwwcdf.pd.infn.it/~loreti/mlo.html
Un. of Padova, Dept. of Physics - Padova, Italy          
lor...@padova.infn.it
/*
 *  getch.c  --  getch(), a blocking single character input from stdin
 *               kbhit(), a keyboard lookahead monitor
 */
/*
 *  getch()  --  a blocking single character input from stdin
 *
 *  This function has been tested on several SysV machines, SunOS4.1,
 *  BSDI UNIX, and Linux.
 *
 *  BSD or POSIX systems may require a struct termios, older systems
 *  may require a struct termio, and some (Linux) may be able to use
 *  either.
 *
 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <termios.h>
#include <sys/ioctl.h>
int getch(void);
int kbhit(void);
 
#define CMIN  1
#ifdef CTIME
#undef CTIME
#endif
#define CTIME 1
/*
 *  get a single character from stdin.
 *
 *  First stdout is flushed, stdin is then switched to 
 *  raw mode and input is waited for.  When a single
 *  character is received from stdin the tty is restored
 *  to its original state and  the character is returned.
 *  If the read from stdin fails, a -1 is returned.
 *
 *  A conditional allows compiling with or without echo.
 */
int
getch(void)
{
  char			ch;
  int			error;
  static struct termios	Otty, Ntty;
  fflush(stdout);
  tcgetattr( 0, &Otty);
  Ntty = Otty;
  Ntty.c_iflag 		= 0;	/* input mode		*/
  Ntty.c_oflag 		= 0;	/* output mode		*/
/* Change this conditional to enable echo of input character */
#if 1
  Ntty.c_lflag 		= 0;	/* line settings (no echo) */
#else
  Ntty.c_lflag 		= ECHO;	/* line settings (echo) */
#endif  
  Ntty.c_cc[VMIN] 	= CMIN;	/* minimum time to wait	*/
  Ntty.c_cc[VTIME] 	= CTIME;/* minimum characters to wait for */
#if 0
/*
 * use this to flush the input buffer before blocking for new input
 */
#define FLAG TCSAFLUSH
#else
/*
 * use this to return a char from the current input buffer, or block if
 * no input is waiting.
 */
#define FLAG TCSANOW
#endif	
 if ( 0 == (error = tcsetattr( 0, FLAG, &Ntty))) {
    error =  read( 0, &ch, 1 );	      /* get char from stdin */
    error += tcsetattr(0, FLAG, &Otty);   /* restore old settings */
  }
  return ( error == 1 ? (int) ch : -1 );
}
/*
 *  kbhit()  --  a keyboard lookahead monitor
 *
 *  returns the number of characters available to read, or -1 on error.
 *
 */
int
kbhit(void)
{
  int			cnt = 0;
  int			error;
  static struct termios	Otty, Ntty;
  tcgetattr( 0, &Otty);
  Ntty = Otty;
  Ntty.c_iflag 		= 0;	   /* input mode		*/
  Ntty.c_oflag 		= 0;	   /* output mode		*/
  Ntty.c_lflag 	       &= ~ICANON; /* raw mode */
  Ntty.c_cc[VMIN] 	= CMIN;	   /* minimum time to wait	*/
  Ntty.c_cc[VTIME] 	= CTIME;   /* minimum characters to wait for */
  if (0 == (error = tcsetattr(0, TCSANOW, &Ntty))) {
    error += ioctl(0, FIONREAD, &cnt);
    error += tcsetattr(0, TCSANOW, &Otty);
  }
  return ( error == 0 ? cnt : -1 );
}
int
main()
{
  int ch;
  int cnt;
  printf("*** Demonstrate kbhit() function ***\n\n");
  printf("You must enter 10 characters:  ");
  fflush(stdout);
  /* collect 10 characters plus allow for a newline */
  while (1) {
    if (10 <= (cnt = kbhit())) {
      break;
    }
  }
  printf("\n");
  /* delete any 11th char, regardless of what it is */
  if (cnt > 9) {
    cnt = 10;
  }
 
  /* fetch the ten characters, and print them */
  printf("The characters entered were:   ");
  while (cnt--) {
    ch = getch();
    printf("%c", ch);
  }
  while (1) {
    printf("\n\n");
    printf("***  Demonstrate getch() function  ***\n\n");
    printf("  Select      Action\n");
    printf("    1         Run this program again.\n");
    printf("    2         Print this menu again.\n");
    printf("    3         Exit this program.\n\n");
    printf("  Enter a selection:  ?\b");
    fflush(stdout);
    while (1) {
      switch (ch = getch()) {
      case '1': printf("\n\n"); main();
      case '3': printf("\n\n"); exit(EXIT_SUCCESS);
      case '2': break;
      default:  continue;
      }
      break;
    }
  }  
  /* flush stdin before returning to the shell command line. */
  while (cnt--) {
    ch = getch();
    if ('\n' == ch || '\r' == ch) {
      break;
    }
  }
  return EXIT_SUCCESS;
}

 }
Meglio
while(!KeyInt11()) {
        /* qui le routine */
       }
return 0 ;
}
E comunque dovresti precisare che la soluzione da te proposta è
assolutamente non standard e quindi non portabile né tra un sistema e
l'altro né tra un compilatore e l'altro.
EMG

Utilizzando la funz. di Gianfranco e la kbhit() ho risolto il mio
problema... finchè resto nel mondo dos/win.
Grazie e alla prossima

problema... finchè resto nel mondo dos/win.
...e del compilatore Borland, probabilmente ;) ...