◄Up► ◄Contents► ◄Index► ◄Back► ─────Run-Time Library─────────────────────────────────────────────────────── /* UNGET.C illustrates getting and ungetting characters from the console. * Functions illustrated include: * _getch _ungetch _putch */ #include <conio.h> #include <stdio.h> #include <ctype.h> void skiptodigit( void ); int getnum( void ); void main() { int c, array[5]; /* Get five numbers from console. Then display them. */ printf( "Enter five numbers:\n" ); for( c = 0; c < 5; c++ ) { skiptodigit(); array[c] = getnum(); printf( "\t" ); } for( c = 0; c < 5; c++ ) printf( "\n\r%d", array[c] ); printf( "\n" ); } /* Converts digit characters into a number until a nondigit is received */ int getnum() { int ch, num = 0; while( isdigit( ch = _getch() ) ) { _putch( ch ); /* Display digit */ num = (num * 10) + ch - '0'; /* Convert to number */ } _ungetch( ch ); /* Put nondigit back */ return num; /* Return result */ } /* Throws away nondigit characters */ void skiptodigit() { int ch; while( !isdigit( ch = _getch() ) ) ; _ungetch( ch ); } -♦-