Thread: Simple program determining min and max value.

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    8

    Simple program determining min and max value.

    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.)

    Code:
    #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;
                }
                else if(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.

    Code:
    #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;
                }
                else if(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!

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    scanf returns the number of items successfully read, not EOF (which is -1 I believe). Since you only read one value you could test for != 0, or simply == 1. Then you get the condition 'while input is a valid number'.
    Last edited by Subsonics; 09-03-2012 at 11:06 PM.

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    8
    Quote Originally Posted by Subsonics View Post
    scanf returns the number of items successfully read, not EOF (which is -1 I believe). Since you only read one value you could test for != 0, or simply == 1. Then you get the condition 'while input is a valid number'.
    I keep reading that everywhere but I'm not sure what it means. How does it relate to my program determining if the input was an integer or not?

  4. #4
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    I would suggest setting the first number read in, to equal min and max. You can compare after that... This makes it to where if you only enter one number in it will successfully display the number as being both min and max.

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by BipolarBear View Post
    I keep reading that everywhere but I'm not sure what it means. How does it relate to my program determining if the input was an integer or not?
    It's related to this thing you said:
    (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.)

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    8
    Quote Originally Posted by Subsonics View Post
    It's related to this thing you said:
    I see. Thank you. Any suggestion to go about making a statement that tells users to enter an integer if they entered a character? I feel like I'm over thinking this

  7. #7
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    why not put the isdigit into the while loop? That way if something other than a number is entered it will exit the loop.

  8. #8
    Registered User
    Join Date
    Sep 2012
    Posts
    8
    Quote Originally Posted by camel-man View Post
    why not put the isdigit into the while loop? That way if something other than a number is entered it will exit the loop.
    Is it not in the while loop?

  9. #9
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    It is but it would be smart to use it in the condition of the while loop instead of checking for EOF.

  10. #10
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    You can't use isdigit() the way you do, semantically, it expects a character (even if it's int typed), but your pass it a number from scanf.
    As suggested above, if scanf("%d"...) == 0, it means either there's no more input or the input can't be parsed as an integer which is what you want.

  11. #11
    Registered User
    Join Date
    Sep 2012
    Posts
    8
    Okay, so this is what I have..
    Code:
    #include<stdio.h>
    #include<limits.h>
    #include<ctype.h>
    
    int main()
    {
        int number=0;
        int min=INT_MAX;
        int max=INT_MIN;
        printf("Enter any set of numbers: ");
        while(scanf("%d",&number)==0)
        {
    
                
                if(number>max)
                {
                    
                    max=number;
                    
                }
                else if(number<min)
                {
    
                    min=number;
                }
                
    
        }
        
        /*else
        {
            printf("Please enter an integer.");
            return 0;
        }*/
        
    
        printf("The max value is %d and the minimum value is %d \n",max,min);
        
    
        return 0;
    }
    The output tells me 2147483647 and -2147483647. Also, the problem I have with while loop is, where do I put my "please enter an integer" part. Obviously 'else' doesn't apply here.

    Sorry if these questions seem completely dumb. First week into my C course, and I haven't gotten my book yet..

  12. #12
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    That loop will run until a legit number is entered, when that happens it will not run. Basically, as long as scanf fails test the input from scanf.

    Your tests can not test for INT_MAX and INT_MIN, since you are scanning into an int it can never contain a value outside of INT_MIN and INT_MAX.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple program, simple problem
    By KAUFMANN in forum C Programming
    Replies: 5
    Last Post: 02-16-2011, 01:16 PM
  2. simple program, simple error? HELP!
    By colonelhogan44 in forum C Programming
    Replies: 4
    Last Post: 03-21-2009, 11:21 AM
  3. Simple program...simple problem?
    By deadherorising in forum C Programming
    Replies: 2
    Last Post: 03-12-2009, 08:37 PM
  4. Simple program, not so simple problem
    By nolsen in forum C++ Programming
    Replies: 2
    Last Post: 01-18-2008, 10:28 AM
  5. Simple Function Pointers - Determining Output
    By Adrian in forum C++ Programming
    Replies: 1
    Last Post: 12-07-2007, 08:10 PM