PDA

View Full Version : Quick C++ question


SexySites
03-20-2002, 10:41 AM
how can i get the length of a integer ...i.e. how many characters it would take up on the screen ...<br /><br />damn i hate formatting c++ programs using cout ! <br /><br />LOL,<br /><br />laters,<br />Chris

shunga
03-20-2002, 12:24 PM
The quick and dirty method might be to convert the integer to ascii - itoa() - and get the length from that using strlen(). I'm sure there's a more elegent way to do it, though. <img border="0" title="" alt="[Wink]" src="wink.gif" />

shunga
03-20-2002, 12:25 PM
elegent = elegant<br /><br />How come we can't correct typos..? <img border="0" title="" alt="[Big Grin]" src="biggrin.gif" />

SexySites
03-20-2002, 12:29 PM
cheers mate appreciated <img border="0" title="" alt="[Wink]" src="wink.gif" /> <br /><br />laters,<br />Chris

Avatar
03-20-2002, 03:41 PM
if your formatting with cout you should use something like ios setw to eg. right align numbers in nice colums.

SexySites
03-20-2002, 04:03 PM
</font><blockquote><font size="1" face="Verdana, Arial">quote:</font><hr /><font size="2" face="Verdana, Arial">Originally posted by Avatar:<br /><strong>if your formatting with cout you should use something like ios setw to eg. right align numbers in nice colums.</strong></font><hr /></blockquote><font size="2" face="Verdana, Arial">im trying to enter align the columns ..so i need to know how long an integer is to format it correctly ..<br /><br />anyone know another way to do this?<br /><br />laters,<br />Chris

porntowers.com
03-20-2002, 04:40 PM
**First be sure to include the string.h header<br /><br />Copy the integer to another variable (so you can still use it to add/sub etc with out typcasting back), type cast the new variable to a string then use the strlen() function.<br /><br /> int length_of_var = strlen(string_variable);<br /> <br />length_of_var would be the length of the integer at this pont you can apply this variable directly in to setw or pressision<br /><br />Copy the integer to another variable (so you can still use it to add/sub etc with out typecasting back), type cast the new variable to a string then use the strlen() function.<br /><br /> int length_of_var = strlen(string_variable);<br /> <br />length_of_var would be the length of the integer at this point you can apply this variable directly in to setw and/or setprecision manipulators.<br /><br />Late,<br /><br />Kyle

porntowers.com
03-20-2002, 04:41 PM
Wow double post neat... <br /><br />my bad

SexySites
03-20-2002, 07:30 PM
cheers towers'o'porn <img border="0" title="" alt="[Wink]" src="wink.gif" /> <br /><br />laters,<br />Chris

Core-Dump
03-20-2002, 08:05 PM
damn you guys are pathetic...<br /><br />len=(i==0)?1:(int)floor(log10(10*i));<br /><br />len is the lenght, i is your integer.<br /><br />this one will work for positive integers only. just use your brain and expand the code if you need it for the negative integers also.

Core-Dump
03-20-2002, 08:08 PM
...and dont forget to include the math.h header

SexySites
03-20-2002, 10:52 PM
</font><blockquote><font size="1" face="Verdana, Arial">quote:</font><hr /><font size="2" face="Verdana, Arial">Originally posted by Core-Dump:<br /><strong>damn you guys are pathetic...<br /><br />len=(i==0)?1:(int)floor(log10(10*i));<br /><br />len is the lenght, i is your integer.<br /><br />this one will work for positive integers only. just use your brain and expand the code if you need it for the negative integers also.</strong></font><hr /></blockquote><font size="2" face="Verdana, Arial">cheers...<br /><br /> <img border="0" alt="[Bounce]" title="" src="graemlins/bounce.gif" /> <br /><br />laters,<br />Chris

phogirl007
03-21-2002, 08:57 PM
Jesus, this sounds hard... <img border="0" alt="[Sleeping]" title="" src="graemlins/sleeping.gif" />

SexySites
03-21-2002, 09:00 PM
</font><blockquote><font size="1" face="Verdana, Arial">quote:</font><hr /><font size="2" face="Verdana, Arial">Originally posted by phogirl007:<br /><strong>Jesus, this sounds hard... <img border="0" alt="[Sleeping]" title="" src="graemlins/sleeping.gif" /> </strong></font><hr /></blockquote><font size="2" face="Verdana, Arial">go2 back to sleep ....<br /><br />wish i could <img border="0" title="" alt="[Big Grin]" src="biggrin.gif" /> <img border="0" title="" alt="[Big Grin]" src="biggrin.gif" /> <br /><br />laters,<br />Chris