|
|
Discussion Forum
scanf conflicting characters remain in the input stream
Next Thread | Thread List | Previous Thread
Start a Thread | Settings
| Details |
Message |
|
Read-Only
Author Peter-Christian Carstens
Posted 19-Apr-2006 15:27 GMT
Toolset ARM
|
 scanf conflicting characters remain in the input stream
Peter-Christian Carstens
hello, there's a little problem with the scanf-function. My program
...
scanf("%u",&A);
...
expects a unsigned integer. But when someone hits a unwanted character, scanf terminates. Any conflicting character remain in the input stream. How to clear the scanf-input-stream? Is there a known workaround for ARM7 ADuC7026)? In C51 you just CLR ?C?charloaded
RET
But how on ARM7? Thanks for advice :-)
|
|
|
Read-Only
Author Hans-Bernhard Broeker
Posted 19-Apr-2006 17:20 GMT
Toolset ARM
|
 RE: scanf conflicting characters remain in the input stream
Hans-Bernhard Broeker
there's a little problem with the scanf-function
Absolutely not. The only problem here is that you haven't learned enough about scanf() before you started using it.
How to clear the scanf-input-stream?
You don't, for two reasons. First, because there is no such thing as a scanf-input-stream. That stream is in no way special to scanf. It is, for all practical intents and purposes, the standard input stream, a.k.a. stdin. Second, because stdin cannot meaningfully be "cleared".
The issue you've stumbled over is the reason that scanf() is very rarely used in production-grade C programs. They use fgets and ssscanf() instead.
|
|
|
Read-Only
Author Patrick Noonan
Posted 20-Apr-2006 10:46 GMT
Toolset ARM
|
 RE: scanf conflicting characters remain in the input stream
Patrick Noonan
It is interesting to note that the Keil C51 does have an input buffer that needs to be cleared.
http://keil.com/support/docs/2017.htm
|
|
|
Read-Only
Author Hans-Bernhard Broeker
Posted 20-Apr-2006 12:26 GMT
Toolset ARM
|
 RE: scanf conflicting characters remain in the input stream
Hans-Bernhard Broeker
the Keil C51 does have an input buffer that needs to be cleared.
That's an over-interpretaion of that support note you found. Yes, it does have a buffer, which you can clear if you want to. But by no means does that imply it needs to be cleared. In a nutshell: if you feel a need to clear stdin, you've quite invariably done something wrong before, i.e. you're now trying to fix symptoms instead of treating the disease.
Basically, scanf() directly into any other format but strings is hardly ever an appropriate means of reading input from a non-deterministic source, e.g. a human operator. I.e. if you don't know for sure that the input will exactly match your scanf() format, every time, then scanf() is the wrong function to use. As I already indicated, error-prone input pretty much has to be read as a string, which you then sscanf().
|
|
|
Read-Only
Author Patrick Noonan
Posted 20-Apr-2006 15:49 GMT
Toolset ARM
|
 RE: scanf conflicting characters remain in the input stream
Patrick Noonan
Hans,
That's an over-interpretaion of that support note you found.
Yes, it does have a buffer, which you can clear if you want to.
But by no means does that imply it needs to be cleared
I think maybe you misinterpreted my post. You need to clear it only (on the C51 scanf) if unwanted characters appear was my intended meaning. I thought that was clear from the context of the thread but maybe not. I certainly was not suggesting that it should be done as a matter-of-course. :)
|
|
|
Read-Only
Author Peter-Christian Carstens
Posted 20-Apr-2006 12:15 GMT
Toolset ARM
|
 RE: scanf conflicting characters remain in the input stream
Peter-Christian Carstens
Now i have a little workaround.
Maybe it is helpful for some user.
while(1)
{
printf("Enter your personal secret Hex-key!\n");
if (scanf("%X",&A)) break;
scanf("%*"); //Clearing scanf input stream
}
:-)
|
|
Next Thread | Thread List | Previous Thread
Start a Thread | Settings
|
|
|