I posted a question previously about what happens in a program when scanf is requesting an integer, if you just keep hitting enter, you can "do it all day". I got some very helpful replies, with a link that I checked out which showed me other great ways to validate input, which I am grateful for. However, I still have the same problem:
Below isn't very fancy code, but I just wanted to conduct a simple test to see if what I learned would work. The good news is it works great if the user enters a character instead of a number, however, if the user hits enter, they can keep hitting it over and over again and the cursor will happily continue to go on down the screen. I know there must be a way to prevent this from happening, because I've run professional programs before (as I'm sure we all have) that were compiled in C, and I can guarantee you that in any of these programs, you can't pull off this enter fiasco. I really want to know how to fix this, because no matter if one day I become able to code an incredible program, it will never look professional anytime someone accidently just hits enter when I'm asking for an integer, and the cursor scrolls on down the screen.
Please help!!Code:#include <stdio.h> int main() { int number; printf("Enter an integer:"); if (scanf("%d", &number) != 1) { printf("You didn't type in a number"); getchar(); } return 0; }