I'm writing a C program that reads an undefined amount of integers from the keyboard. No limits. The program has a loop that reads integers with scanf and stores the lowest and highest value to display. If the user enters a negative number or non integer, the loop ends and displays info (lowest and highest value).
NOTE: I DO NOT WANT USE BUILT IN FUNCTIONS LIKE "ISDIGIT" OR "FGET". No idea what they are and I don't want to "cheat".
I found out that my EOF value is -1. I tried putting "while(scanf("%d", &num) > -1)" in my loop. This just does nothing if you enter a char(keeps reading and never ends loop). It sometimes breaks the loop if you enter a double negative such a "--2". It doesn't work, anyways.
My problem is with my programs confusing behavior with my current code. Sometimes it does not keep or read a zero. Sometimes it only stores and displays the second highest value. Sometimes it display the second to highest or lowest value after I enter an character or symbol to trigger an end to the loop. Other times it works just fine. I tried some more logical statements to force the min to be zero if it's not equal to 1 and less than 1, but that didn't work either.
An example of the weird behavior...
Enter some integer values, EOF to quit...
0
1
2
3
4
e
not an int
The minimum value is 0
and the maximum value is 3
Can anyone explain why this is happening and offer a solution and/or hint? I've been searching and googling for hours now.
Thank You!
Here is my code...
int min;
int max;
int num;
printf(" Enter some integer values, EOF to quit...\n");
scanf("%d\n", &num);
min = max = num;
while(scanf("%d", &num) == 1)
{
if(num > max)
{
max = num;
}
else if(num < min)
{
min = num;
}
scanf("%d\n", &num);
}//while(scanf("%d", &num) == 1);
printf(" not an int \n");
printf(" The minimum value is %d\n", min);
printf(" and the maximum value is %d\n", max);