This is what I have for this particular function so far...
it works perfectly fine if I enter a number. but if i enter a character or letter or something, i was expecting it to reprompt, but instead it goes into an infinite loop of prompting and error messages. Can someone explain why? How can I fix it?Code:unsigned long getNum(char prompt[80]) { unsigned long darts; printf("%s", prompt); while((scanf("%lu", &darts)) != 1) { printf("\n\nThis is not an acceptable entry. please try again.\n"); printf("%s", prompt); } return darts; }



4Likes
LinkBack URL
About LinkBacks



. If scanf is looking for a number, it won't read past any characters, unless they are valid for that type, or a white space. That is, for an unsigned long type, it will skip any leading white space, then try to read digits. If there are no digits, it fails the scan and returns 0, but it does not discard that input.