-3

I am having trouble figuring out how to separate each digit in an integer number. Basically, I have to ask the user what the base number is, and then ask them for two integer numbers. Now, I have the task of checking to make sure each digit in the two integers is smaller than the base number (I have no idea how to do this!).

An example would be something like this:

Enter a base:
3
Enter your first number:
00120
Enter your second number:
11230

I would have to check each digit in the first and second number. Where the first number would be valid because all digits are smaller than 3, and the second number would be invalid because it has a 3 in it which is not smaller than the base.

I've spent multiple hours trying to figure this out on my own and have had no luck.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
xxrachh
  • 1
  • 3
  • You may find the modulo operator, `%` helpful. – user4581301 Nov 08 '18 at 18:50
  • Near duplicate: [C++ get each digit in int](https://stackoverflow.com/questions/4615046/c-get-each-digit-in-int). Ignore the chosen answer. While it does work, the brutal inefficiency of it has earned the downvotes it has received. Focus on the later answers. – user4581301 Nov 08 '18 at 18:54
  • You may find [`std::stoi` and family](https://en.cppreference.com/w/cpp/string/basic_string/stol) helpful. – user4581301 Nov 08 '18 at 19:45
  • Input the number as a string. The numeric value of a digit can be extracted by: `digit_value = numeric_string[i] - '0';` – Thomas Matthews Nov 08 '18 at 20:14

2 Answers2

0

If you're asking for user input, you don't yet have any integers. You have text, and all you need to do is check whether the text contains valid digit characters. As long as you don't get into bases greater than 10, that's simple, because the characters '0' ..'9' are required to be contiguous and increasing, so you can convert a digit character to its numerical value by subtracting '0' from it.

bool is_valid(char ch, int base) {
    return isdigit(ch) && ch - '0' < base;
}
Pete Becker
  • 74,985
  • 8
  • 76
  • 165
  • Please read ISO/IEC 9899:1999 §7.4/1 (alternative: https://stackoverflow.com/questions/17975913/does-ctype-h-still-require-unsigned-char) – Swordfish Nov 08 '18 at 18:59
0

If you are sure that the input does not contain any non-number characters, you can use the % operator to check every digit explicitly. Here is a simple representation of what I mean:

#include <iostream>

bool isValid(int numb, int base) {
  do  {
    if (numb % 10 >= base) { // if the digit cannot be used with this base
      return false;          // the integer is invalid 
    }
  } while (numb /= 10);

  return true;               // if all the digits passed the above test, 
                             // the integer is valid
}

int main() {
  int numb, base;
  std::cin >> numb >> base;

  std::cout << "input " 
    << (isValid(numb, base) ? "is " : "is not ")
    << "valid " << std::endl;
  return 0;
}
Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93