![]() |
|
#1
|
|||
|
|||
Keyboard Buffer QuestionHello all programmer, how to flush keyboard buffer in C++. In c, we can flush the keyboard buffer with this method : fflush(stdin);
How to flush keyboard buffer or fixed my program ? CPP / C++ / C Code:
Please help me. Thanks for your help. Last edited by LuciWiz : 09-May-2007 at 06:41.
Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
|
|
#2
|
|||
|
|||
Re: Keyboard Buffer QuestionQuote:
Here's an easy way, assuming that the user entered a valid integer: CPP / C++ / C Code:
If the user enters something that can't be converted to an integer, the program hangs up, since cin will be in a "fail" state and nothing else can be done with cin until the error flag is cleared. (You should always test for valid input anyhow, in my opinion.) CPP / C++ / C Code:
Regards, Dave |
|
#3
|
|||
|
|||
Re: Keyboard Buffer QuestionMy lecturer has told me before when we scan a character from user, the enter key will go in to keyboard buffer. This will cause next statement undefined behavior.
By the way, any standard C++ function to do so ? Thanks for your help. Your help is greatly appreciated by me and others. |
|
#4
|
|||
|
|||
Re: Keyboard Buffer QuestionQuote:
Quote:
By the way, there is a difference between "undefined behavior" and "misuse of functions that results in incorrect values and/or incorrect program operation." Using fflush(stdin) results in "undefined behavior", since the C standard defines a standard library function, fflush(), that is used for output streams, but does not mention anything about flushing input streams. Microsoft compilers may very well do what you want (that is: empty out the input buffer, including the newline). Other compilers may not (and, in my experience, Borland and GNU compilers do absolutely nothing if you fflush(stdin)). On the other hand, using cin>> to get an integer and then using getline() for the next input (expecting to get a string from the next line of user input) is not undefined behavior. It's a program bug that can be handled easily in a number of ways, all of which are Standard C++ and none of which result in undefined behavior. I gave an example. Quote:
Some examples using standard C++functions and operators: CPP / C++ / C Code:
Regards, Dave Last edited by davekw7x : 10-May-2007 at 09:04.
|
|
#5
|
|||
|
|||
Re: Keyboard Buffer QuestionSorry for my stupidness. I don't know how to ask a smart question. I think i need to check the Eric Raymond's articles.
By the way, thanks very much. Hope GOD will blessed you. |
|
#6
|
|||
|
|||
Re: Keyboard Buffer QuestionQuote:
Regards, Dave |
|
#7
|
|||
|
|||
Re: Keyboard Buffer QuestionHe has published some articles about how to ask a smart question.
You can check at his homepage. Besides that, i also quite admire you because you willing to help people and clear the confusion inside my mind. My hotlink account is peter_wkc_20@hotmail.com . Thanks for your reply. Hope GOD will blesses you. |
|
#8
|
|||
|
|||
Re: Keyboard Buffer QuestionCPP Code :
CPP / C++ / C Code:
Header File : CPP / C++ / C Code:
The same problem which after Enter name, it just directly went to Enter employee ID. How i clear the next line buffer ? The previous of the user enter is not integer. I have cast it. Thanks for your help. Your help is greatly appreciated by me and others. Last edited by admin II : 18-May-2007 at 14:35.
Reason: Please surround your CPP code with [CPP] ... [/CPP]
|
|
#9
|
||||
|
||||
Re: Keyboard Buffer QuestionDon't use cin when reading a mix of strings and numbers. Use getline() and verify the input before using.
The problem you're having is cin is leaving your input buffer dirty because it reads only what it wants. getline() will always read the entire line entered. You just have to delete the '\n' at the end of the line read. And if you just read number, convert it from a string using any way that makes sense (stringstreams, sprintf, atoi, etc.) __________________
Age is unimportant -- except in cheese |
|
#10
|
|||
|
|||
Re: Keyboard Buffer QuestionQuote:
its simple : use std::cin.sync(). CPP / C++ / C Code:
|
Recent GIDBlog
Python ebook by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Buffer size in C | dereliction | C Programming Language | 3 | 05-Feb-2007 17:51 |
| question about inline function | bchasco | C++ Forum | 1 | 22-Jul-2005 15:52 |
| non-member function question | crq | C++ Forum | 1 | 03-Feb-2005 21:59 |
| Bounded Buffering? | pablowablo | C Programming Language | 0 | 17-Jan-2005 05:51 |
| Repetition structure problem and question | brookeville | C++ Forum | 17 | 29-Oct-2004 17:48 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The