Thread
:
Need help with C++ please
View Single Post
Mentalist Traceur
2013-09-20 , 05:17
Posts: 2,225 | Thanked: 3,822 times | Joined on Jun 2010 @ Florida
#
5
Oh, now if you are stuck trying to figure out what the length of "[string 2]" would be, since that's an integer/float/double variable, I would approach it like this:
You know that the amount of digits in the number determines the length.
You know in decimal counting systems, each digit is one greater multiple of 10, essentially.
For an integer, then, you can figure out how many digits it will be by dividing it by ten until it becomes zero (as C++ will floor decimal results for intergers).
For every power of ten it has, you add one more to your digits counter.
With a little bit of thinking, it should be possible for you to figure out how to convert the above into code. If you can't after a while, I can show you what I've written to do so. (Hint, either a loop, or a series of if/else-if checks with less/great -than comparisons.)
With a little more thinking, you can adapt such approaches to floats/doubles as well.
There's other ways too, of course, usually involving using other fancy C++ classes and stuff, if that's the route you want to go.
Quote & Reply
|
The Following 3 Users Say Thank You to Mentalist Traceur For This Useful Post:
bandora
,
Estel
,
Wikiwide
Mentalist Traceur
View Public Profile
Send a private message to Mentalist Traceur
Find all posts by Mentalist Traceur