Advertisement
Guest User

interceptInput() function

a guest
Feb 27th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.12 KB | None | 0 0
  1. int Game::interceptInput(int a_lineNum, int a_lineDrawPoint, const int a_maxDigits, char a_validChars[], int a_validCharLength, eInput a_inputType, int a_minRange, int a_maxRange) {
  2.     bool validEntry = false;
  3.     int digitCount = 0;
  4.     int validCharSum = 0;
  5.  
  6.     //Capture all incoming key-presses until something valid has been entered
  7.     while (!validEntry) {
  8.         if (_kbhit()) {
  9.             char ch = _getch();
  10.  
  11.             //If pressed key is a valid key print it out
  12.             for (int i = 0; i < a_validCharLength; i++) {
  13.                 if (ch != a_validChars[i]) continue;
  14.                 else if (ch == a_validChars[i] && digitCount < a_maxDigits) {
  15.                     DrawChar((a_lineDrawPoint + 1) + digitCount, a_lineNum + 1, ch, CYAN);
  16.                     //Add up appropriately for a number
  17.                     if (a_inputType == NUM || a_inputType == NUM_RANGE) {
  18.                         validCharSum *= 10; //Prepare it for the appropriate value
  19.                         validCharSum += (ch - '0'); //Add the value inside of the char instead of the ASCII code
  20.                     //Add up to get single character
  21.                     } else {
  22.                         validCharSum += ch;
  23.                     }
  24.                     digitCount++;
  25.                 }
  26.             }
  27.  
  28.             //Valid entry is finished, user has hit enter. Now check if its within the appropriate range.
  29.             if (ch == 0x0D && digitCount > 0) {
  30.                 //If we want to check if number input is in a valid range
  31.                 if (a_inputType == NUM_RANGE) {
  32.                     if (validCharSum >= a_minRange && validCharSum < a_maxRange) {
  33.                         DrawTextLine(1, a_lineNum, GREEN, "SUCCESS            ", 100);
  34.                         return validCharSum;
  35.                     }
  36.                     else {
  37.                         DrawTextLine(1, a_lineNum, RED, "NUMBER OUT OF RANGE", 100);
  38.                         digitCount = 0;
  39.                         validCharSum = 0;
  40.                         for (int i = 0; i < a_maxDigits; i++)
  41.                             DrawTextLine((a_lineDrawPoint + i), a_lineNum + 1, WHITE, "\t", 1); // \t gives empty space
  42.                         continue;
  43.                     }
  44.                 }
  45.                 return validCharSum;
  46.             }
  47.  
  48.             //Player has made a mistake, clear space proportionate to max digits
  49.             if (ch == 0x08) {
  50.                 for (int i = 0; i < a_maxDigits; i++)
  51.                     DrawTextLine((a_lineDrawPoint + i), a_lineNum + 1, WHITE, "\t", 1); // \t gives empty space
  52.                 validCharSum = 0;
  53.                 digitCount = 0;
  54.             }
  55.         }
  56.     }
  57.     return 0; //All code paths now return a value
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement