This is a program asking a user to input numbers. It then displays the minimum and maximum values. The first set of code works fine. (Actually, it doesn't work how I'd like to. I run it in linux. I enter the set of numbers. Hit enter and nothing happens, and THEN hit ctrl+d and it shows the solution. That's the only way I can get it to work.)
#include<stdio.h>
#include<limits.h>
#include<ctype.h>
int main()
{
int number=0;
int min=INT_MAX;
int max=-1;
printf("Enter any set of numbers: ");
while(EOF!=scanf("%d",&number)) //Loop finding max and min.
{
if(number>max)
{
max=number;
}
elseif(number<min)
{
min=number;
}
}
printf("The max value is %d and the minimum value is %d \n",max,min);
return 0;
}
Now I altered my code up a bit so if the user inputs a letter, I want to tell them that they must input an integer instead.
#include<stdio.h>
#include<limits.h>
#include<ctype.h>
int main()
{
int number=0;
int min=INT_MAX;
int max=-1;
printf("Enter any set of numbers: ");
while(EOF!=scanf("%d",&number)) //Loop finding max and min.
{
if(isdigit(number)) //If the entered character isn't an integer, tell them to input integer and terminate
{
if(number>max)
{
max=number;
}
elseif(number<min)
{
min=number;
}
}
else
{
printf("Please input an integer.");
return 0;
}
}
printf("The max value is %d and the minimum value is %d \n",max,min);
return 0;
}
So technically neither code works, but the bottom code actually doesn't work at all. Any suggestions? Thanks in advance!
its the line "EOF!=scanf("%d",&number)" and its not doing what you expect it to be doing.
For scanf, if the input does not match the format specifier (the %d) then you discard the input from the stream, meaning you're already ignoring anything that isnt a long int to start off with.