0

I have created a function in C that tests if an integer in between a range of 100 (min) and 500 (max) - The function calls on another function called "getInt()" which tests whether the integer is valid - this part is working fine. My issue is that the when I test, the while loop only runs once - I enter a value not within the range and I get the correct error message and prompt to re-enter - When I enter another value, the program just stops. I am only posting the function code here as the code in the main is correct (was supplied by my college):

int getIntInRange(int min, int max) { int intValue = 0; int flag = 0;

while (flag ==0) {
    intValue = getInt();
    if (intValue < min) {
        printf("*** OUT OF RANGE*** <Enter a number between %d and %d>: ", min, max);
        clearKeyboard();
    }

    else if (intValue > max) {
        printf("*** OUT OF RANGE*** <Enter a number between %d and %d>: ", min, max);
        clearKeyboard();
    }

    else {
        flag = 1;

        } 
} 

return intValue;
0

It's working as described and the loop will stop when the number is between min and max.

You described that the loop runs fine when the input is too large or too small - it will ask for a new number until it gets something in the right range.

When the number IS in the range, it'll set the flag to 1, so the loop will terminate on the next pass. It's working as expected. If you were to put a printf statement just before the return statement, you'd see that the program is working exactly as you want. ;-)

The program "just stops" because once the loop finishes, the program doesn't have anything else to do, except to return the number to the operating system. ;-)

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

3
  • @bbNicky, I created a sandbox running your code (with a couple modifications since I didn't have the other functions) so you can see that it works, just as cliffb has said. bit.ly/32EkDaz run ./se – curiouskiwi Jul 21 '19 at 4:34
  • Thank you very much Cliff B! My issue was that when I entered multiple value out of the range, the loop would not continue. I was playing around with it and thought to comment out the "clearKeyboard()" function since it was an integer and this seems to have solved the problem. – bbNicky Jul 21 '19 at 4:56
  • I'd have to see the clear keyboard function to know what it was doing. Assuming that it wasn't causing the problem, the code above would simply end when it hit the return statement. If there was code that followed, removing the return statement would cure the issue. However, if clearKeyboard() is ending processing, then you'd need to resolve that issue separately. – Cliff B Jul 21 '19 at 19:18
0

So after you get an error message that says "Out of Range", when you try to enter another value that is out of range, the loop stops. There is a "clearKeyboard()" function that is called in here - this is to clear the buffer of the "\n" character as it creates problems for strings in C using scanf. After struggling with this for a good while, I thought about the clearKeyboard() function and noted that because we are entering integers and not strings, we might not need it. So I removed it from the function and now it works like a charm. You can enter "Out of Range" values as much as you want and the loop will keep working. Here is the updated code:

int getIntInRange(int min, int max) { int intValue = 0; int flag = 1;

while (flag == 1) {
    intValue = getInt();
    if (intValue < min  || intValue > max) {
        printf("*** OUT OF RANGE*** <Enter a number between %d and %d>: ", min, max);
        //clearKeyboard();                      The clearKeyboard() function is what kept this loop from working - must be because it was an integer
    }
    else  {
        return intValue;
        flag = 0;   
    }                   
}  

}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .