>>but isn't that a problem with scanf then?
Yes, it is a problem with scanf. But it is not unique to scanf. The same problem can occur with any input function that reads a single character and does not treat whitespace specially.
>>but isn't that a problem with scanf then?
Yes, it is a problem with scanf. But it is not unique to scanf. The same problem can occur with any input function that reads a single character and does not treat whitespace specially.
I disagree. It isn't a problem "with scanf", but rather with the way people use scanf. Most people don't use it correctly. When you tell scanf to read one character, it does. That's all. Nothing more. The fact remains that your input us two characters, and you aren't using it right.
If you use scanf correctly, it can be used for input without problems. However, as it's been stated, most people don't use it right. It, scanf, is simply a tool. Most people don't understand how it works, so they get odd results from using this tool.
In other words, it's not the tool's fault you aren't using it correctly. It's yours.
Quzah.
Hope is the first step on the road to disappointment.
Yes, you are right. I should have been more specific so as to avoid bashing the tool. However if the design of a function does not lend itself to being used correctly then it most certainly is a problem with the function in my opinion. scanf has too many subtle pitfalls for me to not consider it a problematic function, and many people seem to agree.
I agree, but everyone around here likes to post with "-pedantic", and I didn't want to feel left out.Originally Posted by Princeton
Quzah.
Hope is the first step on the road to disappointment.
Originally Posted by quzah
I think agree there. Many ppl use C today and forget that it was desgned quite some time ago, and the functions were not necessarily desgined to be used as one might think. Scanf is excellent for doing what its name implies scan formated input. So your previous statement is on point. if you use it as its meant to be used then there is no problem. Unfourtunetly until the way C is taught changes many ppl will still be using it for early input until they get to more "user freindly" functions, and it will continue to be an initiation period for n00bies in learning its subtleties. Not necessarily a bad expereince just hair pulling.
@princton: I don't think scanf is flawed or was designed incorrectly. What we expect from it is incorrect, the majority of the time. Functions only do what you tell them to. its our fault if we expect other wise and use them unlike how they were meant to be.
my $0.02
cheers.
Warning: Opinions subject to change without notice
The C Library Reference Guide
Understand the fundamentals
Then have some more fun
hi again. just to update, i found out a solution to my problem. all i had to do was add a space before the %c in the scanf statement.
this makes the scanf read all the white space before it gets to the character. thanks for the help guys.Code:scanf(" %c", &x);