1. ## Beginner C++ problem

I need a program that prints the "hundreds" digit in a series of integers entered by the user. For instance, if the user enters 2356 and 764 and 894, then the program will display 3, 7, and 8.

2. I do not want code, just an idea, concept or explanation of how to do it. I want to put the code together myself. I just don't know who to determine the correct digit.

3. ((int) n / 100) % 10
or
((int) n % 1000) / 100

%: modulus operator ('remainder on division by')

4. I see how these expressions would work with an integer of four or more digits, and I greatly appreciate that. But how about three digit integers? I'm not supposed to use if statements yet. And thanks again.

5. OK. Let's try 386:

(int) 386 / 100 = 3
3 % 10 = 3

or

(int) 386 % 1000 = 386
386 / 100 = 3

See? It works.

Note that the (int) cast applies to n, not to (n/100)
This is in case n is a single; of course I'd expect it to be an int already so that's a bit unnecessary: in fact

(i / 100) % 10

will work just as well; because both i and 100 are integers, C++ will use integer division and return an integer.

Oh, and my cryptic reference to speed: thinking about it, both will be equal in speed (one integer division, one integer modulus), but the first looks tidier. On the other hand, if n is of type long, then the following code:

(int)(n % 1000) / 100

will be fastest as it involves one long modulus and an integer division as opposed to

(n / 100) % 10

which would involve a long modulus and a long division.

Hope that makes sense.

6. Of course you're right - I was confusing the quotient with the modulus thinking the latter would be 0 given 386%1000. When in doubt, read the book.

Thanks once more.

Originally posted by strangerstill
OK. Let's try 386:

(int) 386 / 100 = 3
3 % 10 = 3

or

(int) 386 % 1000 = 386
386 / 100 = 3

See? It works.

Note that the (int) cast applies to n, not to (n/100)
This is in case n is a single; of course I'd expect it to be an int already so that's a bit unnecessary: in fact

(i / 100) % 10

will work just as well; because both i and 100 are integers, C++ will use integer division and return an integer.

Oh, and my cryptic reference to speed: thinking about it, both will be equal in speed (one integer division, one integer modulus), but the first looks tidier. On the other hand, if n is of type long, then the following code:

(int)(n % 1000) / 100

will be fastest as it involves one long modulus and an integer division as opposed to

(n / 100) % 10

which would involve a long modulus and a long division.

Hope that makes sense.

7. Happy to be of help. Hope you get further with C++ than I did.

8. is there source code for this program?? if theres, would you please post it. thank you

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts

New Security Features Planned for Firefox 4
Another Laptop Theft Exposes 21K Patients' Data
Oracle Hits to Road to Pitch Data Center Plans
Microsoft Preps Array of Windows Patches
Microsoft Nears IE9 Beta With Final Preview
Simplified Analytics Improve CRM, BI Tools
Android Passes RIM as Top Mobile OS in 2Q