# C++ simple question

Discussion in 'Software Development' started by The G-man, Oct 14, 2003.

Not open for further replies.
1. ### The G-manThread Starter

Joined:
Jun 3, 2001
Messages:
140
I am writing an app where I want to have the user enter an 8-digit integer value. From that value, I would like to be able to extract certain digits from the number they entered to be used in an algorithm to generate a code. Is there a simple way of doing this without using string functions? I would prefer to manipulate the entered value as a single integer.

Thanks!

2. ### Gibble

Joined:
Oct 9, 2001
Messages:
27,087
12345678

Take the number

If I want to find out the value in the 100s spot (6) I can as follows.

12,345,678 MOD 1000 = 678
678 MOD 100 = 78
678 - 78 = 600
600 / 100 = 6

So basically, it's a little tricky, but very doable.

--HTH

3. ### Gibble

Joined:
Oct 9, 2001
Messages:
27,087
Or to reduce that to a single formula should be

((X MOD 1000) - ((X MOD 1000) MOD 100)) / 100

To find the digit in the 1000s spot, just add a 0 to each part of the formula

ie
((X MOD 10000) - ((X MOD 10000) MOD 1000)) / 1000

4. ### The G-manThread Starter

Joined:
Jun 3, 2001
Messages:
140
Thanks for your help. I figured that there was a fairly simple way!

Joined:
Oct 9, 2001
Messages:
27,087
No problem

As Seen On

### Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

over 733,556 other people just like you!