Is there an equivalent of 'substr' for integers

May 20, 2019 at 5:27am
Hey guys, I was wondering if you knew a of an equivalent of 'substr' for integers. So for example, if a user input a credit card / debit card number and you want to detect what kind of card it is based on the first number the integer begins with (ex - Master Card starts for '5', Visa begins within '4' and etc) how would you do that? Thank you


May 20, 2019 at 5:43am
Well you wouldn't input CC/DC numbers as integers to begin with.

You'd input them as strings, and use substr().
May 20, 2019 at 6:12am
Ok, I'll try it again and see if it works. Thanks
May 20, 2019 at 7:42am
Hello kdrewes,

salem c's suggestion works for using "substr()", but if all you want is the first number then cardNum[0] is all you need. Just keep in mind this is a "char" not an "int".

Andy
Last edited on May 20, 2019 at 7:43am
May 20, 2019 at 9:25am
Just in case you keep that Master Card and Visa numbers as integer not as a string (recommended), to pick a single digit / and % (integer divide and modulo) may help. 5432 / 1000 = 5. Or the 2nd digit from the RHS: (5432 % 100) / 10 = 3

Edit: see underscored
Last edited on May 20, 2019 at 2:00pm
May 20, 2019 at 1:12pm
integers are not stored in base 10 inside computers so no, there is no 'get the digit' feature because the 'digit' is a bit, not a base 10 digit. If you want the bit, you can have that, or a subset of them, all that is easy. You have to do the math as Mike showed above, or similar, or convert it to string or always have it as a string.
May 21, 2019 at 3:39am
Ok sounds good thank you guys for all the help. I'm going to play around with it and see what I can come up with. I think I'll originally try to use ints and then go on to strings and see what I can come up with. Thank you again for all the help.
May 21, 2019 at 9:41am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// Example program
#include <iostream>
#include <string>
using namespace std;

unsigned char Nth_Digit(long long no, unsigned char dgt=0)
{
    if (no < 0) no = -no;
    while (dgt > 0)
    {
        no /= 10;
        --dgt;
    }
    return no % 10;
}

int main()
{
    long long number;
    int digit;
    number = 100025; digit = -1; // invalid digit
    cout << "\nNumber: " << number << ", digit: " << digit << ", figure: " << (unsigned) Nth_Digit(number, digit);
    number = -100025; digit = 1;
    cout << "\nNumber: " << number << ", digit: " << digit << ", figure: " << (unsigned) Nth_Digit(number, digit);
    number = 100025; digit = 0;
    cout << "\nNumber: " << number << ", digit: " << digit << ", figure: " << (unsigned) Nth_Digit(number, digit);
}

Number: 100025, digit: -1, figure: 0 << silently wrong
Number: -100025, digit: 1, figure: 2
Number: 100025, digit: 0, figure: 5

Just a suggestion, filters for invalid input missing.

Edit: To return the digit unsigned char is sufficiently enough, alas cout refuses to display it. Probably there is also a canonical method, I guess.
Last edited on May 21, 2019 at 10:09am
Topic archived. No new replies allowed.