-
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.
-
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.
-
Member
((int) n / 100) % 10
or
((int) n % 1000) / 100
Your choice: first probably faster.
%: modulus operator ('remainder on division by')
-
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.
-
Member
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.
-
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.
-
Member
Happy to be of help. Hope you get further with C++ than I did.
-
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
Forum Rules
|
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
VMware Updates Hyperic System Management
File Monitoring Key to Enterprise Security
LinkedIn Snaps Up SaaS Player mSpoke
|
Bookmarks