2
Contributors
1
Reply
15
Views
2 Years
Discussion Span
Last Post by rubberman
1

It puts the last character read from the input buffer back into the buffer so the next call to getch() will return it. Let's say you are reading numbers into a string buffer from stdin and you get a character. You want to process the numeric data and then start reading a string, so you put the character back with ungetch(). That way when you start reading a string, you start with the character you had read. Example where you are reading amounts and currency type and your incoming data looks like this:

100dollars
200euros
300pesos

So you read the 100, and then get the 'd'. You put it back in the buffer and then read the currency, starting with the 'd', getting "dollars", then 200 and 'e', putting the 'e' back and getting "euros", etc.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.