Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Microsoft C 6.0 - <b>ungetch() push back last character read from the console</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
 ungetch()               Push Back Last Character Read from the Console

 #include   <conio.h>                    Required for declarations only

 int        ungetch(c);
 int        c;                           Character to be pushed

    ungetch() pushes back 'c', the last character read from the console.
    'c' then becomes the next character read.  ungetch() can only be
    called once between read statements.

    Returns:    If successful, the character 'c'.  On error, EOF is
                returned.

 Portability:   Applies to MS DOS only.

   -------------------------------- Example ---------------------------------

    The following statements get digits and alphabetic characters from
    the console.

           #include <conio.h>
           #include <ctype.h>

           char item[80];
           char qty[5];
           int x, y;
           char ch;

           main()
           {
               y = 0;
               printf("ENTER QUANTITY: ");
               while (isdigit(ch = getche()) && y < 5)
                     qty[y++] = ch;
               qty[y] = '\0';
               /* process 'qty' */
               if (isalpha(ch))
                  ungetch(ch);
               x = 0;
               printf("\nENTER ITEM: ");
               while (isalpha(ch = getche()) && x < 80)
                     item[x++] = toupper(ch);
               item[x] = '\0';
               /* process 'item' */
               printf("\n%s %s\n",qty,item);
            }


See Also: ungetc() cscanf() getch() getche()

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson