skip to content »

C programming validating input

c programming validating input-40

I just need to understand functions and how they work a little better. The user could input "10abc" and the input will be accepted (as the integer '10') and leave the (presumed) garbage in the input.This is from a thread where the user was concerned about that potential extra garbage.

You should be able to return 0; instead of exit(0).The exit(0) I added to terminate, should it not be there?The reason for it was so that the program terminated rather than clearing the buffer etc, I had confusion on clearing input when the input is tallying for a grand total, so I found it a bit easier for myself to just terminate until I grasped the concept.Stdin is a file stream that is automatically opened for you when your program runs. Functionality rules and clarity matters; if you can work a little elegance in there, you're stylin'. It is "standard input", from whence comes user keyboard input or files opened with command line redirection. If you can't spell "u", "ur", and "ne1", why would I hire you? Then again, you should be fixing the cin and letting the user try to input another number.

Thanks, ill need to think on this of what your doing, not familiar with std:: method but i guess i understand it. The solutions given so far make assumptions about the input.

Validating user input is part of the normal functionality of the program, not an exceptional situation.

It's on the lazy side to use exceptions here; you're basically treating it as a .

Here, I am writing a User Define Function to take integer input with minimum and maximum values validation.

If there is no validation and we enter an invalid value, program produces incorrect output (because there is no option to go back and enter the value again).

I have tried 2 different compilers with 2 different results, so at this point I decided I need some help. That doesn't mean everything is ducky and you can just try again. Under the conditions shown, you should be able to successfully clear junk from the stream, also. I didn't look at your entire program, just the post. You should check the documentation on the things you use, guesses are dangerous if you're, say, designing medical equipment. The teacher knows nothing about C and likes to talk to us about . = 1 can mean you didn't get good input and you can ask again, or it can mean (if you got EOF) that the stream is broken and won't return even GOOD input if you call it again.