First, thanks for all the help with my other questions, learning a lot fairly quickly. I read basically when you get an error from input, the error stays until you clear it and enters an infinite loop (this describes EXACTLY the problem I was having). So I'm trying to clear it using clearerr() but I can't seem to figure out what I'm trying to clear. I thought I understood but I don't, I commented out the various combinations I've tried but don't work. The error occurs if I enter something like ff55. If I enter 55lb or 503lb or 4kg the program works fine.
Code:
#include <stdio.h>
#include <string.h>
int main()
{
int currentNumber;
int testForEOF;
char currentUnit[3];
printf("Enter a number between 0-200 followed by it's units: ");
testForEOF = scanf("%d%2s", ¤tNumber, currentUnit);
while (testForEOF != -1)
{
if (strcmp("lb", currentUnit) == 0 && currentNumber >= 0 && currentNumber <= 200)
{
printf("Valid lbs\n");
}
else if (strcmp("kg", currentUnit) == 0 && currentNumber >= 0 && currentNumber <= 100)
{
printf("Valid kgs\n");
}
else
{
/* I've tried
clearerr(testForEOF);
clearerr(&testForEOF);
clearerr(*testForEOF);
clearerr();
clearerr(stdin);
clearerr(scanf);
*/
printf("Invalid format\n");
}
testForEOF = scanf("%d%s", ¤tNumber, currentUnit);
}
return (0);
}