1

This function asks for an integer between loLimit and hiLimit and allows the user to safely enter it. It keeps asking until the user enters a number in the legal range. Then it returns the legal number.

The correct output should be:
Please enter an integer between 32 and 127: 19
Please enter an integer between 32 and 127: 128
Please enter an integer between 32 and 127: 48
Please enter an integer between 48 and 127: 47
Please enter an integer between 48 and 127: 65

However, my output behaves strangely, it seems as if the loLimit value changes back and forth. Also, numbers are only recorded after entered twice.

Please enter an integer between 32 and 127: 2
Please enter an integer between 32 and 127: 150
Please enter an integer between 32 and 127: 56
Please enter an integer between 32 and 127: 56
Please enter an integer between 56 and 127: 66
Please enter an integer between 32 and 127: 66
Please enter an integer between 66 and 127: 77

Here's my code:

int enterNumber (int loLimit, int hiLimit){
    int min;
    int max;

    do {
    printf("Please enter an integer between %d and %d:\n", loLimit, hiLimit);
    scanf("%d", &min);
    }
    while (min<loLimit || min>hiLimit);
    return min;
}

int main(){
    enterNumber(32,127);
    int min=enterNumber(32,127);

    enterNumber(min,127);
    int max=enterNumber(enterNumber(32,127),127);


}
1
  • The scanf function can fail so you should always test its result (number of successfully scanned items) Commented Jun 20, 2015 at 6:02

1 Answer 1

3

You call the enterNumber() function 5 times so 5 accepted values are taken :

  enterNumber(32,127);
  Please enter an integer between 32 and 127: 56 
  1st accepted number

  int min=enterNumber(32,127);
  Please enter an integer between 32 and 127: 56  // 2nd one
  2nd accepted number

  enterNumber(min,127);
  Please enter an integer between 56 and 127: 66
  3rd accepted answer.

  int max=enterNumber(enterNumber(32,127),127);    //called twice
  Please enter an integer between 32 and 127: 66
  Please enter an integer between 66 and 127: 77
  4th and 5 th accepted answer 

You can correct your logic by deleting the first and third enternumber call as shown .

   int enterNumber (int loLimit, int hiLimit){
   int min;
   int max;

   do {
      printf("Please enter an integer between %d and %d:\n", loLimit, hiLimit);
      scanf("%d", &min);
      }
   while (min<loLimit || min>hiLimit);
   return min;
  }

  int main(){
 // enterNumber(32,127); comment out this
  int min=enterNumber(32,127);

  // enterNumber(min,127);
   int max=enterNumber(min,127);
   int no=enterNumber(min,max);

}

First it accepts a minimum value , then a maximum and finally enters a no between this range.

Sign up to request clarification or add additional context in comments.

3 Comments

Oh yea thats right! I commented out both line. However, when I entered the first legal value, the low and high range still prompt the same value, it only changes the second time I entered a legal value. Any ideas?
Check it now @Katherine.It enters a no between min and max finally.
this doesn't quite compile, because the compiler will raise a warning about the unused variable 'max' in the function: enterNumber();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.