First mistake: you didn't use code tags. There's more information in my signature.
void main()
{
Don't use void main. More info in my signature. :cheesy:
To make sure that the user entered what you were expecting, always check the return value of scanf, as it returns the number of read items from the input buffer. If it's 0 or less than 0, you need to ask the user to reenter the data.
Hope this helps
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
I would suggest getting input as a string so that you can detect invalid characters. scanf() won't necessarily do that for you. For example you might type "123.45abcd" and scanf() will accept everything up to the first non-numeric character -- 'a' -- without any complaints. But if you want to produce a warning or error to the user then you should get the input as a string (such as call fgets() ) and validate each character.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
I have been struggling with this program assignment for weeks now. Can someone offer some advice on how to only accept numerical input, and to reject anything else? Here is my code.
For the purpose you are trying to achieve, I wouldn't recommend scanf. It offers so very little control over what the user enters and also validation becomes a pain. Better accept the entire input as string and write your own validation functions. More info here .
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734