Thread: How can I use the same loop multiple times in a function

  1. #1
    Registered User
    Join Date
    Apr 2017
    Posts
    6

    How can I use the same loop multiple times in a function

    Hello,

    I am currently self learning how to use functions to better my programming abilities.

    I am trying to create a program that uses a function, checks if the user input is between 2 values and prints it out.

    I have a total of Four values i want to input. ie. 1,2,3,4

    Each of the Four values has separate "low" "high" values.

    ie. between "1" and "9" or between "0" and "9" etc..

    In the end it should print something like this

    value-1

    "the value you have entered is: 1"

    value-2

    "the value you have entered is: 2"

    value-3

    "the value you have entered is: 3"

    value-4

    "the value you have entered is: 4"



    My problem right now is trying to figure out how i can advance to the next print statement in the loop with a new range of "high" and "low" values.


    here is my code so far.

    Code:
    #include <stdio.h>
    
    
    int getUserInput(int, int);
    
    
    int main(){
        
        
        getUserInput(1, 9);
        getUserInput(0, 9);
        getUserInput(0, 9);
        getUserInput(1, 8);
    
    
    
    
        return 0;
    }
    
    
    int getUserInput(int low, int high) {
     
        int goodValue;
    
    
         while(goodValue < low || goodValue > high) {
             printf("Enter a value between 1 and 9 inclusive: ");
             scanf("%d", &goodValue); 
    
    
         if (goodValue >= low && goodValue <= high){
             printf("the value you have entered is:  %d\n", goodValue);
    
    
         }
      
        else {
             printf("***error: %d is not valid, number must be between 1 and 9, try again***\n", goodValue);
        }
    
    
    }
            return goodValue;
     
    }
    Last edited by acoustix; 04-12-2017 at 02:03 PM.

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    I don't understand what you mean by "how i can advance to the next print statement in the loop with a new range of "high" and "low" values." Don't the four function calls do that?

    Also, your while should be a do/while (with the test at the bottom) since the way you have it goodValue is uninitialized the first time the test is encountered.

  3. #3
    Registered User
    Join Date
    Apr 2017
    Posts
    6
    Quote Originally Posted by algorism View Post
    I don't understand what you mean by "how i can advance to the next print statement in the loop with a new range of "high" and "low" values." Don't the four function calls do that?

    Also, your while should be a do/while (with the test at the bottom) since the way you have it goodValue is uninitialized the first time the test is encountered.
    wow i feel like such a moron... it works with a do while loop now, thanks for the obvious pointer

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    That indentation totally messed with my brain... Here's the "correct" way to indent:
    Code:
    #include <stdio.h>
    
    int getUserInput(int, int); 
    
    int main()
    {
        getUserInput(1, 9);
        getUserInput(0, 9);
        getUserInput(0, 9);
        getUserInput(1, 8);
     
        return 0;
    }
    
    int getUserInput(int low, int high)
    {
        int goodValue;
    
        while(goodValue < low || goodValue > high) {
            printf("Enter a value between 1 and 9 inclusive: ");
            scanf("%d", &goodValue); 
    
            if (goodValue >= low && goodValue <= high) {
                printf("the value you have entered is:  %d\n", goodValue);
            } else {
                printf("***error: %d is not valid, number must be between 1 and 9, try again***\n", goodValue);
            }
        }
    
        return goodValue;
    }
    Anyway, to the matter at hand. What you probably mean you want is for the function to print the correct range, correct? For that, you need to change the printfs that use literal values and make them use the low and high parameters.
    Devoted my life to programming...

  5. #5
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    A common C idiom for what you are trying to do is this:
    Code:
    int getValue(int low, int high) {
        int value = 0;
    
        for (;;) {
            printf(">>> ");
            scanf("%d", &value);
    
            if (value >= low && value <= high)
                break;
    
            printf("Bad value\n");
        }
    
        return value;
    }
    But the scanf will get caught in an infinite loop if the user starts their input with letters instead of numbers. So it's even better to read an entire line with fgets and then scan it with sscanf:
    Code:
    int getValue(int low, int high) {
        char line[1000]; // give it lots of space!
        int value = 0;
    
        for (;;) {
            printf(">>> ");
            if (fgets(line, sizeof line, stdin) == NULL)
                break; // return on eof or error (or exit or whatever)
    
            if (sscanf(line, "%d", &value) == 1
                && value >= low && value <= high)
                break;
    
            printf("Bad value\n");
        }
    
        return value;
    }
    This still doesn't catch bad input like 1234xyz or out-of-range input. That takes a little more work (using strtol).
    Last edited by algorism; 04-12-2017 at 03:46 PM.

Popular pages Recent additions subscribe to a feed

Tags for this Thread