Jul 2, 2010 at 8:40pm UTC
Is this the proper way to put a floating number in an array?
char s[25];
sprintf(s,"%4.1f",winpercent*100.0);
Jul 3, 2010 at 12:10am UTC
does the way i did it work properly tho?
thanks.
Jul 3, 2010 at 12:43am UTC
Only if winpercent is below a certain size, otherwise you'll have a buffer overflow.
Jul 3, 2010 at 1:03am UTC
winpercent is always between 0 and 1, so by size do u mean if it has more than 25 digits beyond the decimal point (since s is of size 25)?
and if so, your way resolves that issue?
Jul 3, 2010 at 3:58pm UTC
i added
#include <sstream>
#include <iostream>
to my visual c++ file, yet i get the error
'stringstream' : undeclared identifier
what am i missing?
Jul 3, 2010 at 4:22pm UTC
You need to fully qualify the name: std::stringstream
Or insert an using declaration before using the name: using std::stringstream;
Jul 3, 2010 at 6:24pm UTC
i'm getting the following error:
'setprecision' : is not a member of 'std'
on this line:
ss << std::fixed << std::setprecision(1) << tempy;
setprecision is a member, so what did i do wrong?
Jul 3, 2010 at 7:47pm UTC
still not there yet. i get an error that "s" is now of wrong type when i try to use TextOut.
std::stringstream ss;
ss << std::fixed << std::setprecision(1) << tempy;
std::string s;
s=ss.str();
dc.TextOut(x,y,s,4);
originally, i had s as
char s[25].
Jul 3, 2010 at 7:52pm UTC
Try dc.TextOut(x,y,s.c_str() ,4);
Jul 3, 2010 at 8:26pm UTC
almost there. ;)
i do this operation in a loop:
std::stringstream ss;
std::string s;
for(z=1;z<10;z++){
ss << std::fixed << std::setprecision(1) << winpercent[z]*100.0;
s=ss.str();
dc.TextOut(x,y,s.c_str(),4);
}
s does not change beyond the first loop. do i need to reset it somehow?
Jul 3, 2010 at 8:38pm UTC
Actually, you just need to reset ss every loop.
Jul 4, 2010 at 9:13pm UTC
does this code require any release of memory in terms of memory leaks?
thanks.
Jul 4, 2010 at 9:33pm UTC
No. Typically you'll only need to release memory manually if you use new or malloc().
Jul 5, 2010 at 12:14am UTC
i'm getting an error when the number to be texted is below 10, like 9.8. i guess by specifying "4" in the text out it tries to add the hundreth numeral, which on-screen comes out as a square box. how can i make sure it only texts to the tenth of a point?
Jul 5, 2010 at 12:17am UTC
By passing the real length of the string.
Replace 4 by s.length().
Jul 5, 2010 at 12:49am UTC
but the real length could vary hugely, as in 1/6. isn't there a comparable method to %4.1f using this method so i know it will only go to tenths?
Jul 5, 2010 at 1:10am UTC
This part: ss << std::fixed << std::setprecision(1) << winpercent[z]*100.0;
is like the "4.1" stuff. After you have done that, the string contains only the parts parts you wanted.