-1

I'm trying to set this function up so that it waits for the user to press a key on the keypad and then returns the number that they pressed.

This is what I've got but it seems to be allowing anything to be returned.

int numpad(int min, int max) {
  int input;
  do {
    input=getch() - 48;
  }
  while(input<=min && input>=max);
  return input;
}

Anyone know what's going wrong here?

iBug
  • 35,554
  • 7
  • 89
  • 134
  • What's intended to do with `while(input<=min && input>=max)`? – iBug Nov 25 '17 at 03:22
  • say min=0 and max=9. it checks if the key pressed is one of the wanted numerical values. – Ben MacPherson Nov 25 '17 at 03:24
  • My god! How did you suppose it's CSS or a JavaScript snippet?! – iBug Nov 25 '17 at 03:25
  • Please try to avoid the use of [*magic numbers*](https://en.wikipedia.org/wiki/Magic_number_(programming)). If by `48` you mean the [ASCII](http://en.cppreference.com/w/c/language/ascii) encoded character `'0'` then *say* so. That will also make the code portable (there are systems which doesn't use ASCII). – Some programmer dude Nov 25 '17 at 03:26
  • @Someprogrammerdude Don't worry. This guy's obviously programming for Windows. – iBug Nov 25 '17 at 03:27

1 Answers1

0

I guess you wrote a wrong token:

while(input<=min && input>=max);
                 ^^

Which should have been

while(input< min || input> max);
            ^    ^^       ^
iBug
  • 35,554
  • 7
  • 89
  • 134